android HTTP post方法时,如何使用cookies
16lz
2021-01-23
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //set cookie. sCookie is my static cookie string if(sCookie!=null && sCookie.length()>0){ conn.setRequestProperty("Cookie", sCookie); } // Send data OutputStream os = conn.getOutputStream(); os.write(mData.getBytes()); os.flush(); os.close(); // Get the response! int httpResponseCode = conn.getResponseCode(); if (httpResponseCode != HttpURLConnection.HTTP_OK){ throw new Exception("HTTP response code: "+httpResponseCode); } // Get the data and pass them to the XML parser InputStream inputStream = conn.getInputStream(); Xml.parse(inputStream, Xml.Encoding.UTF_8, mSaxHandler); inputStream.close(); //Get the cookie String cookie = conn.getHeaderField("set-cookie"); if(cookie!=null && cookie.length()>0){ sCookie = cookie; } /* many cookies handling: String responseHeaderName = null; for (int i=1; (responseHeaderName = conn.getHeaderFieldKey(i))!=null; i++) { if (responseHeaderName.equals("Set-Cookie")) { String cookie = conn.getHeaderField(i); } }*/ conn.disconnect();
http://stackoverflow.com/questions/3152740/android-post-data-and-cookies
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer