java httpclient form_HttpClient基于表单登录

本篇文章帮大家学习HttpClient基于表单登录,包含了HttpClient基于表单登录使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。

使用HttpClient库,可以发送请求或通过传递参数登录表单。

按照以下步骤登录表单。

第1步 - 创建一个HttpClient对象HttpClients类的createDefault()方法返回类CloseableHttpClient的对象,该对象是HttpClient接口的基本实现。使用此方法创建一个HttpClient对象 -

CloseableHttpClient httpClient = HttpClients.createDefault();

第2步 - 创建RequestBuilder对象

RequestBuilder类用于通过向其添加参数来构建请求。如果请求类型是:PUT或POST,则它将参数作为URL编码实体添加到请求中

使用post()方法创建RequestBuilder对象(类型为POST)。

//Building the post request object

RequestBuilder reqbuilder = RequestBuilder.post();

第3步 - 将Uri和参数设置为RequestBuilder。使用RequestBuilder类的setUri()和addParameter()方法将URI和参数设置为RequestBuilder对象。

//Set URI and parameters

RequestBuilder reqbuilder = reqbuilder.setUri("http://httpbin.org/post");

reqbuilder = reqbuilder1.addParameter("Name", "username").addParameter("password", "password");

第4步 - 构建HttpUriRequest对象设置所需参数后,使用build()方法构建HttpUriRequest对象。

//Building the HttpUriRequest object

HttpUriRequest httppost = reqbuilder2.build();

第5步 - 执行请求CloseableHttpClient对象的execute()方法接受HttpUriRequest(接口)对象(即:HttpGet,HttpPost,HttpPut,HttpHead等)并返回响应对象。

通过将它传递给execute()方法来执行前面步骤中创建的HttpUriRequest。

//Execute the request

HttpResponse httpresponse = httpclient.execute(httppost);

示例

以下示例演示了如何通过发送登录凭据登录到表单。在这里,我们向表单发送了两个参数 - username和password,并尝试打印消息实体和请求的状态。

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpUriRequest;

import org.apache.http.client.methods.RequestBuilder;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.net.URISyntaxException;

public class FormLoginExample {

public static void main(String args[]) throws Exception {

//Creating CloseableHttpClient object

CloseableHttpClient httpclient = HttpClients.createDefault();

//Creating the RequestBuilder object

RequestBuilder reqbuilder = RequestBuilder.post();

//Setting URI and parameters

RequestBuilder reqbuilder1 = reqbuilder.setUri("http://httpbin.org/post");

RequestBuilder reqbuilder2 = reqbuilder1.addParameter("Name",

"username").addParameter("password", "password");

//Building the HttpUriRequest object

HttpUriRequest httppost = reqbuilder2.build();

//Executing the request

HttpResponse httpresponse = httpclient.execute(httppost);

//Printing the status and the contents of the response

System.out.println(EntityUtils.toString(httpresponse.getEntity()));

System.out.println(httpresponse.getStatusLine());

}

}

执行上面示例代码,得到以下结果:

{

"args": {},

"data": "",

"files": {},

"form": {

"Name": "username",

"password": "password"

},

"headers": {

"Accept-Encoding": "gzip,deflate",

"Connection": "close",

"Content-Length": "31",

"Content-Type": "application/x-www-form-urlencoded; charset = UTF-8",

"Host": "httpbin.org",

"User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_91)"

},

"json": null,

"origin": "217.216.145.180",

"url": "http://httpbin.org/post"

}

HTTP/1.1 200 OK

Cookie表单登录

如果表单存储cookie,而不是创建默认的CloseableHttpClient对象。通过实例化BasicCookieStore类来创建CookieStore对象。

//Creating a BasicCookieStore object

BasicCookieStore cookieStore = new BasicCookieStore();

使用HttpClients类的custom()方法创建HttpClientBuilder。

//Creating an HttpClientBuilder object

HttpClientBuilder clientbuilder = HttpClients.custom();

使用setDefaultCookieStore()方法将cookie存储设置为客户端构建器。

//Setting default cookie store to the client builder object

Clientbuilder = clientbuilder.setDefaultCookieStore(cookieStore);

使用build()方法构建CloseableHttpClient对象。

//Building the CloseableHttpClient object

CloseableHttpClient httpclient = clientbuilder1.build();

通过传递执行请求来构建上面指定的HttpUriRequest对象。如果页面存储cookie,则传递的参数将添加到cookie存储中。

可以打印CookieStore对象的内容,在其中查看参数(以及以前存储的页面)。

要打印cookie,请使用getCookies()方法从CookieStore对象获取所有cookie。此方法返回List对象。使用Iterator打印列表对象内容,如下所示 -

//Printing the cookies

List list = cookieStore.getCookies();

System.out.println("list of cookies");

Iterator it = list.iterator();

if(it.hasNext()){

System.out.println(it.next());

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值