Java Post接口调用
程序员文章站
2024-01-07 22:07:52
(1)通过Cookies.properties管理cookiescookies.key1 = .AspNetCore.Sessioncookies.key2 = TS01d2d863cookies.key3 = ssoinfocookies.key4 = ssoinfo24cookies.value1 = CfDJ8ANxMHG%2FRqFCldmuIDAedzPQCxXH%2F4MQOziHM6ylx68sZ52B83DlrCAa8FBRRZnBP6f%2BbL2nBdE%2BZcb5sSX...
(1)通过Cookies.properties管理cookies
cookies.key1 = .AspNetCore.Session
cookies.key2 = TS01d2d863
cookies.key3 = ssoinfo
cookies.key4 = ssoinfo24
cookies.value1 = CfDJ8ANxMHG%2FRqFCldmuIDAedzPQCxXH%2F4MQOziHM6ylx68sZ52B83DlrCAa8FBRRZnBP6f%2BbL2nBdE%2BZcb5sSXLGAKUwjsNNoPgnUzC1Y4ODUGmDNaTLzBnfMFr3LuelaNamu8e4rJ9L9oq61fmmRDxqgPZnZ11n8l3UT%2BsABTFcXp4
cookies.value2 = 01b0c3cbf0a7a43acf6c9195af9724460a7a4e6e2fb9757dca95571026ffb0ec5dd58fc95401577230300c321889910ad61e8a3dc95f436fffbd9fde73b4acc52ec807d274f14b0308814f3316f382b96c8143c47e33d8ef84e797f3e0d668873b2f922d1d
cookies.value3 = 80002344
cookies.value4 = 80002344
cookies.domain = www.domian.com
(2)编码
package org.domain.project.api;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* 接口访问认证
*/
public class PostTest {
private static HttpClient httpClient = new DefaultHttpClient();
private static HttpPost post;
private static HttpResponse response;
private CookieStore cookieStore;
private String result = null;
private String url = null;
private String param = null;
private ResourceBundle bundle;
@BeforeTest
public void initCookies() throws IOException {
cookieStore = new BasicCookieStore();
bundle = ResourceBundle.getBundle("cookies", Locale.CHINA);
String key1 = bundle.getString("cookies.key1");
String key2 = bundle.getString("cookies.key2");
String key3 = bundle.getString("cookies.key3");
String key4 = bundle.getString("cookies.key4");
String value1 = bundle.getString("cookies.value1");
String value2 = bundle.getString("cookies.value2");
String value3 = bundle.getString("cookies.value3");
String value4 = bundle.getString("cookies.value4");
String domain = bundle.getString("cookies.domain");
BasicClientCookie cookie1 = new BasicClientCookie(key1, value1);
cookie1.setDomain(domain);
cookie1.setPath("/");
BasicClientCookie cookie2 = new BasicClientCookie(key2, value2);
cookie2.setDomain(domain);
cookie2.setPath("/");
BasicClientCookie cookie3 = new BasicClientCookie(key3, value3);
cookie3.setDomain(domain);
cookie3.setPath("/");
BasicClientCookie cookie4 = new BasicClientCookie(key4, value4);
cookie4.setDomain(domain);
cookie4.setPath("/");
cookieStore.addCookie(cookie1);
cookieStore.addCookie(cookie2);
cookieStore.addCookie(cookie3);
cookieStore.addCookie(cookie4);
}
@Test()
public void getChannelInfo(){
url = "https://www.domain.com/getChannlInfo";
param = "{\"PageIndex\":1,\"PageSize\":15,\"Subcmt\":\"xxxxx\",\"SeqNo\":\"TX-000000\"}";
}
@Test()
public void getUserCheckInfo(){
url = "https://www.domian.com/rds/DeviceBase/GetDeviceInfos";
param = "{PageIndex: 1, PageSize: 15, InstalStartDate: \"\", InstalEndDate: \"\", DueStartdate: \"\", DueEndDate: \"\"}";
}
@AfterTest
public void doPost(){
post = new HttpPost(url);
JSONObject jsonObject = JSONObject.parseObject(param);
StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
try {
response = httpClient.execute(post);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文地址:https://blog.csdn.net/qq969887453/article/details/110846595