第一种:
public void success(){
PropertyConfigurator.configure("log4j.properties");
PostMethod postMethod = new PostMethod(url);
//httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//dff98220f294f4c7c4f92f2e2cacf644
NameValuePair[] data = {
new NameValuePair("key","123456"),
new NameValuePair("word",proutil1.GetPro("word"))
};
postMethod.setRequestBody(data);
HttpClient httpClient=new HttpClient();
try {
String result;
int code=httpClient.executeMethod(postMethod);
Assert.assertEquals(code,200);
result=postMethod.getResponseBodyAsString();
JSONObject jsonObject=new JSONObject(result);
String reason=jsonObject.getString("reason");
int error_code=jsonObject.getInt("error_code");
Assert.assertEquals(reason,"success");
Assert.assertEquals(error_code,0);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
```
第二种:
```java
public void test_Success(){
PropertyConfigurator.configure("log4j.properties");
String login_url= Resourse_Bunder.get_url("order","login1.url");
System.out.println(login_url);
System.out.println("===============");
HttpPost httpPost=new HttpPost(login_url) ;
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
JSONObject param=new JSONObject();
logger.debug("传入参数");
param.put("username","admin");
param.put("password","123456");
StringEntity entity= null;
try {
entity = new StringEntity(param.toString(),"gbk");
logger.debug("设置请求头");
httpPost.setHeader("content-type", "application/json");
httpPost.setHeader("token","mocktoken9gd878ad");
httpPost.setEntity(entity);
String result = null;
logger.debug("执行httpPost获得响应结果");
HttpResponse response= httpClient.execute(httpPost);
result= EntityUtils.toString(response.getEntity(),"gbk");
System.out.println(result);
httpClient.close();