import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class HttpYSBIS {
/**
* 发送POST请求HttpMethod
*
* @param url
* 请求的地址
* @param parameter
* 请求的参数
* @return
* @throws Exception
*/
public static String getPostMethod(String url, String parameter) throws Exception {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
try {
RequestEntity entity = new StringRequestEntity(parameter.replaceAll("\+", "%2B"), "application/json","UTF-8");
// application/x-www-form-urlencoded方式是Jquery的Ajax请求默认方式,这种方式的好处就是浏览器都支持,在请求发送过程中会对数据进行序列化处理
post.setRequestHeader("Content-Type","application/json;charset=UTF-8");
post.setRequestEntity(entity);
// 使用 POST 方式提交数据
client.executeMethod(post);
// 打印服务器返回的状态
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 打印结果页面
String response = new String(post.getResponseBodyAsString().getBytes("UTF-8"),"UTF-8");
new String(post.getResponseBodyAsString());
// 打印返回的信息
post.releaseConnection();
return response;
}
public static void main(String[] args) {
String str="{'dateStr':'2019-04-03 16:31','OAdh':'LFDJ20190403001','userId':'180312139','message':'金牛游戏有限公司000(111)'}";
try {
String a = HttpYSBIS.getPostMethod("接口地址及方法",str);
System.out.println(a);
} catch (Exception e) {
e.printStackTrace();
}
}
}