1、需要的jar 包有 json 需要的有 json-lib-2.4-jdk15.jar 、ezmorph-1.0.6.jar、commons-logging-1.1.3.jar、commons-lang-2.6.jar、commons-collections-3.2.1.jar、commons-beanutils-1.9.2.jar
2、http 请求需要的jar
httpclient-4.3.6.jar、httpcore-4.3.3.jar、httpmime-4.3.6.jar
3、测试代码
/**
*
*/
/**
* @author 89003422
*
*/
package TestPost;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
public class postDemo {
/**
* 定义所需的变量
*/
private static HttpClient httpClient = new DefaultHttpClient();
private static HttpPost httppost;
private static HttpResponse response;
private HttpEntity entity;
private String postResult = null;
public static void main(String[] args) {
String loginURL = "需要测试的接口地址";
// 创建一个httppost请求
httppost = new HttpPost(loginURL);
JSONObject jsonParam = new JSONObject();
jsonParam.put("mobile", "15627898765");
jsonParam.put("password","e10adc3949ba59abbe56e057f20f883e");
try {
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");// 解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httppost.setEntity(entity);
response = httpClient.execute(httppost);
String strResult = EntityUtils.toString(response.getEntity());
System.out.println("查看返回的结果:" + strResult);
} catch (Exception e) {
e.printStackTrace();
}
httppost.releaseConnection();
}
}