java httpurlconnection 发送cookie时,cookie要在Post前发送
来源:互联网 发布:联通软件研究院待遇 编辑:程序博客网 时间:2024/06/07 20:25
public InputStream getStream(URL url,String post,URL cookieurl){ HttpURLConnection connection; String cookieVal = null; String sessionId = ""; String key=null; if(cookieurl!=null){ try{ connection = (HttpURLConnection)cookieurl.openConnection(); for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++ ) { if (key.equalsIgnoreCase("set-cookie")) { cookieVal = connection.getHeaderField(i); cookieVal = cookieVal.substring(0, cookieVal.indexOf(";")); sessionId = sessionId+cookieVal+";"; } } InputStream in = connection.getInputStream(); System.out.println(sessionId); }catch(MalformedURLException e){ System.out.println("url can't connection"); return null; }catch(IOException e){ System.out.println(e.getMessage()); return null; } } try { connection = (HttpURLConnection)url.openConnection(); //这个要写在Post前,否则会取不到值,原因我不知道 if(cookieurl!=null){ connection.setRequestProperty("Cookie", sessionId); } if(post!=""){ connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.getOutputStream().write(post.getBytes()); connection.getOutputStream().flush(); connection.getOutputStream().close(); } int responseCode = connection.getResponseCode(); int contentLength = connection.getContentLength(); // System.out.println("Content length: "+contentLength); if (responseCode != HttpURLConnection.HTTP_OK ) return(null); InputStream in = connection.getInputStream(); return(in); } catch(Exception e) { // System.out.println(e); // e.printStackTrace(); return(null); } }
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- 发送cookie
- 在JAVA中使用HttpURLConnection发送POST请求
- Python3发送post请求,自动记住cookie
- Java 用HttpClient发送Cookie
- Java利用HttpURLConnection发送post请求
- JAVA使用原始HttpURLConnection发送POST数据
- Java利用HttpURLConnection发送post请求
- Java利用HttpURLConnection发送post请求
- android xutils cookie 发送
- 浏览器发送cookie规则
- 使用HttpURLConnection发送POST请求
- HttpUrlConnection 发送POST请求数据
- C#模拟Post和Get方式发送数据 保持COOKIE
- WebMagic 如何设置 cookie, 以及发送 post 请求
- js中undefined,null,NaN的区别
- 1012
- UART
- 4.2.2 计算数据
- asp.net 日期控件 My97DatePicker
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- 传智播客-ejb3(1)-理解应用服务器、分布式应用和ejb服务
- [C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)
- eclipse调试以及step into step over step return区别
- ubuntu10.10的cvs安装教程总结
- 传智播客-ejb3(2)-bean类型、jboss基本应用、本地组件和远程组件及其底层实现原理简介
- 多路复用器
- 端口映射帮助文档
- 【转】8个高质量图标的最佳搜索引擎