public String test_post1() {
String url = \"http://xxxx\";
byte[] requestBytes;
String result = \"\";
try {
JSONObject js = new JSONObject();
js.put(\"aaa\", \"ccc\");
js.put(\"bbb\", \"111\");
js.put(\"ccc\", \"222\");
js.put(\"ddd\", \"323\");
String string = js.toString();
requestBytes = string.getBytes(\"utf-8\");
HttpClient httpClient = new HttpClient();// 客户端实例化
PostMethod postMethod = new PostMethod(url);
// 设置请求头Authorization
// postMethod.setRequestHeader(\"Authorization\", \"Basic \" +
// authorization);
// 设置请求头 Content-Type
postMethod.setRequestHeader(\"Content-Type\", \"application/json\");
InputStream inputStream = new ByteArrayInputStream(requestBytes, 0,
requestBytes.length);
RequestEntity requestEntity = new InputStreamRequestEntity(
inputStream, requestBytes.length,
\"application/json; charset=utf-8\"); // 请求体
postMethod.setRequestEntity(requestEntity);
httpClient.executeMethod(postMethod);// 执行请求
InputStream soapResponseStream = postMethod
.getResponseBodyAsStream();// 获取返回的流
byte[] datas = null;
datas = readInputStream(soapResponseStream);// 从输入流中读取数据
result = new String(datas, \"UTF-8\");// 将二进制流转为String
// 打印返回结果
System.out.println(result);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // 将参数转为二进制流
catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



