android 中设置http请求头headers的Cookie
来源:互联网 发布:珠海航展歼20知乎 编辑:程序博客网 时间:2024/05/22 02:04
为了验证的安全, 有些应用 会对headers设置cookie 才能进行以后的操作。网上查阅了很多资料,自己也走了些弯路 总算搞出来了 。直接上代码
class NetThr { public static String sessionid=null; //请求的url ,json数据, 服务的类型(获取cookie) public void netThr(String svrurl, String message,int svrType) {String svraddr = ""; svraddr = svrurl.replace(" ", "%20");URL svr = new URL(svraddr);if (message == null) message = "";try {HttpURLConnection httpreq = (HttpURLConnection) svr.openConnection();httpreq.setConnectTimeout(60000);httpreq.setReadTimeout(60000);if (message.length() > 0)httpreq.setRequestMethod("POST");elsehttpreq.setRequestMethod("GET");httpreq.setDoInput(true);if (message.length() > 0) {httpreq.setDoOutput(true); httpreq.setRequestProperty("Charset", "UTF-8");httpreq.setRequestProperty("Content-length", "" + message.getBytes("UTF-8").length);httpreq.setRequestProperty("Content-Type", "application/json");}if (sessionid!=null&&svrType!=HandleRespFun.API_TYPE_GETCOOKIE){ //设置Cookie 到 Headers httpreq.setRequestProperty("Cookie",sessionid);}httpreq.connect();if (message.length() > 0) {OutputStream os = httpreq.getOutputStream();DataOutputStream dos = new DataOutputStream(os);dos.write(message.getBytes("UTF-8"));dos.close();}//返回的headers 例子//Content-Typeapplication/json // Set-Cookieapi_authenticated=1 // Set-Cookieuser_authenticated=1 // Set-CookieJSESSIONID=16i51l7jwmodpb1u1jfpdk0pg;Path=/laundry // ExpiresThu, 01 Jan 1970 00:00:00 GMT // Content-Length175 // ServerJetty(9.2.3.v20140905) // DateTue, 27 Jan 2015 02:32:09 GMT //获取Cookie if(svrType==HandleRespFun.API_TYPE_GETCOOKIE){Map<String, List<String>> kk = httpreq.getHeaderFields();List<String> cook = kk.get("Set-Cookie");if(cook!=null){for (String onecook : cook) {Log.v("Jashon", "Cookie is:" + onecook);if (onecook.contains("JSESSIONID")){ //sessionid=JSESSIONID=16i51l7jwmodpb1u1jfpdk0pgsessionid=onecook.split(";")[0];}}}}}}之前的时候 截取 sessionid部分截取错了 这里要注意一下哦。代码有点乱,不过主要方法逻辑 还是清晰的 希望能帮到一些盆友 自己以后也会用得着
0 0
- android 中设置http请求头headers的Cookie
- 妹子图爬虫,最重要的是请求头headers设置'Referer':'http://www.mzitu.com/'
- HTTP消息头(HTTP headers)-常用的HTTP请求头与响应头
- HTTP消息头(HTTP headers)-常用的HTTP请求头与响应头
- 在AngularJs中怎么设置请求头信息(headers)及不同方法的比较
- http协议学习-请求头Request Headers
- python爬虫设置请求消息头(headers)
- Retrofit2 + OkHttp3下统一设置Http请求的Headers
- Retrofit2 + OkHttp3下统一设置Http请求的Headers
- Android的MediaPlayer在播放时设置Http请求头
- http 请求头设置
- jsp中设置http的P3P头实现跨域设置cookie
- jsp中设置http的P3P头实现跨域设置cookie
- http协议学习系列(请求头---Request Headers)
- JS不允许手动设置请求头的cookie
- Android带Cookie的Http请求
- Android Http请求头与响应头的学习
- Android WebView添加Cookie和添加请求的头
- 关于gradle旧版本升级到1.0新版本注意的一些事。
- Jquery ajax请求后台成功,但没返回值
- hadoop 配置中的几个小笔记
- 解决:Adb connection Error:远程主机强迫关闭了一个现有的连接
- 禁用Eclipse update index
- android 中设置http请求头headers的Cookie
- 子贡问时
- Oracle 统计某一年中的1-12个月的数据总和(非常实用)
- Form.Close 方法[C#]
- 【转】图文详解YUV数据格式
- android系统设置VPN要求输入密码
- 仿网易新闻的导航条及其新闻类别选择
- 汇编调用C函数时的堆栈变化
- hash表