模拟发送带cookies的http请求的两种方法
来源:互联网 发布:金百福软件基本视频 编辑:程序博客网 时间:2024/06/17 03:11
如果想发送带cookies的请求,有两种方式,一种使用工具,一种使用java代码,干货如下:
使用工具
使用的工具是postman和Postman Interceptor使用谷歌浏览器的扩展程序下载(需要科学上网或者修改本机host,不过此方法稍微麻烦点)
使用postman发送带cookies的请求,必须启动谷歌浏览器和postman两者的Interceptor,缺一不可
首先必须得启用浏览器和postman的Interceptor,然后就和正常发postman的请求一样,postman会直接读取谷歌浏览器中的cookies并且和自己的get/post请求一起发送出去
java代码
使用代码发送,其实也很简答。这里给出一种最简单的方法
使用header头参数发送cookies
public static String doPostCookie( String url){ try { HttpPost request = new HttpPost( url ); request.addHeader("Cookie","JSESSIONID=4A3998E6FCA477D878BFF99C26FB1608"); return execute( request ); } catch( Exception e ) { throw new RuntimeException( String.format( "http post fail[message=%s]", e.getMessage() )); } }
其中request.addHeader就是添加一个cookies。此cookies在浏览器中的显示如下:
(此处查看cookies的插件也是谷歌浏览器的一个插件叫editthiscookie)
上面代码的excute方法如下:
private static String execute( HttpUriRequest request ) { try { HttpResponse response = HTTP_CLIENT.execute( request ); StatusLine statusLine = response.getStatusLine(); if( null == statusLine ) { throw new RuntimeException( "http request fail, no status line"); } if( statusLine.getStatusCode() != HttpStatus.SC_OK ) { throw new RuntimeException(String.format( "http request fail[status=%d|message=%s]", statusLine.getStatusCode(), EntityUtils.toString( response.getEntity(), CONTENT_CHARSET ))); } return EntityUtils.toString( response.getEntity(), CONTENT_CHARSET ); } catch( RuntimeException ex ) { LOGGER.error(String.format("execute http request fail[url=%s|msg=%s]", request.getURI(), ex.getMessage())); throw ex; } catch( Exception ex ) { LOGGER.error( String.format( "http request fail[msg=%s|url=%s|param=%s]", ex.getMessage(), request.getURI(), JSON.toJSONString( request.getParams() ) ) ); throw new RuntimeException("http request fail"); } finally { if( null != request && !request.isAborted() ) { request.abort(); } } }
需要导入的包:
<!--http --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.2.2</version> </dependency> <dependency> <groupId>org.jodd</groupId> <artifactId>jodd-http</artifactId> <version>3.7</version> </dependency> <!--http -->
over。。。。。。。。(可以加qq475804848交流技术哦····)
附:
另外如果想让浏览器帮你设置一个cookies,可以使用下面方法:
//设置cookie
response.addHeader(“Set-Cookie”, “uid=112; Path=/; HttpOnly”);
//设置多个cookie
response.addHeader(“Set-Cookie”, “uid=112; Path=/; HttpOnly”);
response.addHeader(“Set-Cookie”, “timeout=30; Path=/test; HttpOnly”);
//设置https的cookie
response.addHeader(“Set-Cookie”, “uid=112; Path=/; Secure; HttpOnly”);
- 模拟发送带cookies的http请求的两种方法
- 发送HTTP请求的两种方式
- Postman模拟发送带token的请求
- 发送请求cookies的处理
- Postman发送带cookie的http请求
- Python发送带header的http请求
- AFnetWorking带Cookies的请求
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- linux命令发送http的get或post请求(curl和wget两种方法)
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- 以DELETE方式发送带请求体的HTTP请求
- Android模拟发送按键功能的两种方法
- .net发送post请求的两种方法
- .net发送post请求的两种方法
- android 打造万能keyStore
- 网站收藏
- React Native 每日一学(Learn a little every day)
- CMS内容管理系统开发- Java Web开发及发布实例(2)—使用JSP实现动态数据交互
- 【面试题】两种方法实现strcpy
- 模拟发送带cookies的http请求的两种方法
- 多态
- 获取执行计划的六种方法
- React Native 学习资源精选仓库
- 在Android开发中eclipse里无NDK选项如何解决?
- 新oj压力测试及水题娱乐赛(水水更健康)---题解
- 用JS获取地址栏参数的方法(超级简单)
- 【Python数据挖掘课程】九.回归模型LinearRegression简单分析氧化物数据
- Android 使用SVG矢量图