热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

androidcookie读写,如何在Android上使用Cookie发出http请求?

由于不建议使用Apache库,因此对于那些想使用的人HttpURLConncetion,我编写了此类,以借助以下答案发送Get和PostR

由于不建议使用Apache库,因此对于那些想使用的人HttpURLConncetion,我编写了此类,以借助以下答案发送Get和Post Request :

public class WebService {

static final String COOKIES_HEADER = "Set-COOKIE";

static final String COOKIE = "COOKIE";

static COOKIEManager msCOOKIEManager = new COOKIEManager();

private static int responseCode;

public static String sendPost(String requestURL, String urlParameters) {

URL url;

String response = "";

try {

url = new URL(requestURL);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(15000);

conn.setConnectTimeout(15000);

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

if (msCOOKIEManager.getCOOKIEStore().getCOOKIEs().size() > 0) {

//While joining the COOKIEs, use ',' or ';' as needed. Most of the server are using ';'

conn.setRequestProperty(COOKIE ,

TextUtils.join(";", msCOOKIEManager.getCOOKIEStore().getCOOKIEs()));

}

conn.setDoInput(true);

conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();

BufferedWriter writer = new BufferedWriter(

new OutputStreamWriter(os, "UTF-8"));

if (urlParameters != null) {

writer.write(urlParameters);

}

writer.flush();

writer.close();

os.close();

Map> headerFields = conn.getHeaderFields();

List COOKIEsHeader = headerFields.get(COOKIES_HEADER);

if (COOKIEsHeader != null) {

for (String COOKIE : COOKIEsHeader) {

msCOOKIEManager.getCOOKIEStore().add(null, HttpCOOKIE.parse(COOKIE).get(0));

}

}

setResponseCode(conn.getResponseCode());

if (getResponseCode() == HttpsURLConnection.HTTP_OK) {

String line;

BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

while ((line = br.readLine()) != null) {

response += line;

}

} else {

response = "";

}

} catch (Exception e) {

e.printStackTrace();

}

return response;

}

// HTTP GET request

public static String sendGet(String url) throws Exception {

URL obj = new URL(url);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// optional default is GET

con.setRequestMethod("GET");

//add request header

con.setRequestProperty("User-Agent", "Mozilla");

/*

* https://stackoverflow.com/questions/16150089/how-to-handle-COOKIEs-in-httpurlconnection-using-COOKIEmanager

* Get COOKIEs form COOKIEManager and load them to connection:

*/

if (msCOOKIEManager.getCOOKIEStore().getCOOKIEs().size() > 0) {

//While joining the COOKIEs, use ',' or ';' as needed. Most of the server are using ';'

con.setRequestProperty(COOKIE ,

TextUtils.join(";", msCOOKIEManager.getCOOKIEStore().getCOOKIEs()));

}

/*

* https://stackoverflow.com/questions/16150089/how-to-handle-COOKIEs-in-httpurlconnection-using-COOKIEmanager

* Get COOKIEs form response header and load them to COOKIEManager:

*/

Map> headerFields = con.getHeaderFields();

List COOKIEsHeader = headerFields.get(COOKIES_HEADER);

if (COOKIEsHeader != null) {

for (String COOKIE : COOKIEsHeader) {

msCOOKIEManager.getCOOKIEStore().add(null, HttpCOOKIE.parse(COOKIE).get(0));

}

}

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(

new InputStreamReader(con.getInputStream()));

String inputLine;

StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {

response.append(inputLine);

}

in.close();

return response.toString();

}

public static void setResponseCode(int responseCode) {

WebService.responseCode = responseCode;

Log.i("Milad", "responseCode" + responseCode);

}

public static int getResponseCode() {

return responseCode;

}

}



推荐阅读
author-avatar
了不起的老狐_226
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有