代码需验证后进行修改才可使用,目前可使用此代码进行请求,根据问题进行修改。
import HttpStatus = org.apache.http.HttpStatus;//视情况删除
import org.apache.http.StatusLine;
import CloseableHttpResponse = org.apache.http.client.methods.CloseableHttpResponse;//视情况删除
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import CloseableHttpClient = org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class PostUrl{
public String post_invoke(){
//接口地址
String url = "http://10.0.37.16:6080/srm/userloginservlet";
String params = "{\"pageSize\":2000,\"currentPage\":1,\"roleId\": \"srm_accessWhiteList\",\"filterString\" :\"\"}";
//模拟请求
HttpClients httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
try{
//String 转换为JSON
StringEntity paramsEntity = new StringEntity(params);
httpPost.setHeader('Content-Type', 'application/json;charset=utf-8');
httpPost.setEntity(paramsEntity);
Object resultObject = httpClient.execute(httpPost);
StatusLine status = res.getStatusLine();
if (status.getStatusCode() == 200) {
String result = EntityUtils.toString(result.getEntity(), 'utf-8');
}else{
println('接口请求失败:'+status);
}
}catch(Exception e){
System.out.println(e);
}
return result;
}
}