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

由于不建议使用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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值