HttpClient方法:
private static void httpClientPost() {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(TEST_URL);
try {
ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write("");
writer.flush();
}
};
post.setEntity(new EntityTemplate(cp));
HttpResponse response = client.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
本文提供了一个使用 HttpClient 进行 POST 请求的 Java 示例代码。通过创建 DefaultHttpClient 和 HttpPost 对象,并设置 ContentProducer 来发送请求。此外,还展示了如何处理响应。
1万+

被折叠的 条评论
为什么被折叠?



