Httpclient模拟POST请求调用接口

public class HttpClient {


private static final Logger logger = LogR.getLogger(TCFGDemo.class.getName());

public static void main(String[] args) throws Exception {

  getCWMSData(); 

}


public static void getCWMSData() throws Exception {

  String requestParameter = "入参";

  HttpClient httpclient = new HttpClient();

  httpclient.getParams().setContentCharset("UTF-8");


  String url = "调用接口的URL";

  PostMethod m = new PostMethod(url);

  m.addRequestHeader("Accept", "application/json");

// m.addParameter(paramName, paramValue);

  m.addParameter("requestParameter", requestParameter);

// RequestEntity entity = new StringRequestEntity(str,"application/json", "UTF-8");

// m.setRequestEntity(entity);

  m.addRequestHeader("Authorization", "用固定的token");

// m.addRequestHeader("Authorization", getToken());  动态生成token

  int a = httpclient.executeMethod(m);

  System.out.println(a);

  if (a == 200) {

  String res = m.getResponseBodyAsString();

  System.out.println(res);

  System.out.println(1);

  } else {

  System.out.println(m.getResponseBodyAsString());

  System.out.println("error===" + a);

  System.out.println("url===" + url);

  }

}

}

你可能感兴趣的:(Httpclient模拟POST请求调用接口)