转自http://stackoverflow.com/questions/3152740/android-post-data-and-cookies
in Java HttpURLConnection you can set / get a cookie this way (here is the whole connection process):
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();
分享到:
相关推荐
Android Studio实现POST请求,每个步骤都有注释,比较适合萌新了解如果通过POST方式发起网络请求。
android webview模拟网页post操作
POST应用之COOKIES管理(可应用于多线程及多COOKIES管理)
android HttpClient post上传文件
android基础 POST GET 读取后台语言(PHP JSP)的程序!
Laravel开发-no-post-data-laravel-middleware 在某些情况下,当从表单发布数据时,Web服务器可能会丢失发布数据。如果用户上载的大型文件超出了Web服务器配置中设置的大小限制,则通常会发生这种情况。Laravel无法...
能够收集并显示页页深层信息的软件。它不用代理服务器或一些复杂的网络监控工具,就能够在显示网页同时显示网页请求和回应的日志信息。甚至可以显示浏览器缓存和IE之间的交换信息。集成在Internet Explorer工具栏。
c#下post 发送 multipart/form-data和JSON数据
android原生post请求源码更多请查看我的博客:https://blog.csdn.net/xiayiye5
一个简单的android小程序,基于post请求获取json内容并加以解析,代码极小,但是关键问题已经解决,有需要的话可以自己加以改编
and it allows you to read and post even when you are offline. For Android v.7.0 devices. Under Free/Libre and Open Source License. For more information please read the AndStatus Wiki. See the ...
Android post 服务器 代码 还不错 可以开发成通用性的接口
postData.php
【最新】C++ http post form_data传文件源码,【最新】C++ http post form_data传文件源码,【最新】C++ http post form_data传文件源码
Android中Https请求get和post 博客地址:http://blog.csdn.net/lsf1025995457/article/details/51794377
android中post获取json,你懂得,还是比较详细的
Android客户端post上传字段和多张图片 c#接收
此demo演示android通过get和post请求方法同服务器交互,测试需要tomcat,具体参考 http://blog.csdn.net/youmingyu/article/details/52524538
描述了http协议下post提交的方法
c#模拟post发送json和multipart/form-data格式