Java如何使用HttpClient进行POST请求呢?
下文笔者讲述HttpClient使用POST请求提交表单数据的方法及示例分享
1.设置代理
HttpHost proxy = new HttpHost("8.8.8.8", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
2.设置目标地址
HttpPost httppost = new HttpPost("http://www.java265.net/login.jsp");
System.out.println("请求: " + httppost.getRequestLine());
3.构造最简单的字符串数据
StringEntity reqEntity = new StringEntity("username=test&password=test");
4.设置类型
reqEntity.setContentType("application/x-www-form-urlencoded");
5.设置请求的数据
httppost.setEntity(reqEntity);
6.请求
httpclient.execute
例:HttpClient提交POST请求的示例分享
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* HttpClient使用POST方式提交普通表单数据
*/
public class HttpClientPost {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 代理的设置
HttpHost proxy = new HttpHost("8.8.8.8", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// 目标地址
HttpPost httppost = new HttpPost("http://www.java265.net/login.jsp");
System.out.println("请求: " + httppost.getRequestLine());
// 构造最简单的字符串数据
StringEntity reqEntity = new StringEntity("username=test&password=test");
// 设置类型
reqEntity.setContentType("application/x-www-form-urlencoded");
// 设置请求的数据
httppost.setEntity(reqEntity);
// 执行
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


