Android的调用setRequestProperty在url.openConnection() [英] Android setRequestProperty in a url.openConnection()

查看:1499
本文介绍了Android的调用setRequestProperty在url.openConnection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序需要设置一个requestproperty在连接。这里是我的code:

I have an Android app that need to set a requestproperty in a connection. Here is my code:

 URL url = new URL(sUrl);

 HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
 connection.setRequestProperty("cookie", cookievalue);
 connection.connect();

当我称之为调用setRequestProperty 方法,它启动异常:

When I call the setRequestProperty method it launch the exception:

java.lang.IllegalStateException: Cannot set request property after connection is made

有没有一种方法来创建无需使用 url.openConnection连接到文件()

推荐答案

您可以尝试使用CookieManager中的 http://developer.android.com/reference/java/net/HttpURLConnection.html

You could try to use the CookieManager mentioned in http://developer.android.com/reference/java/net/HttpURLConnection.html

设置cookie来CookieManager

Set your cookie to CookieManager

    CookieManager cookieManager = new CookieManager();
    CookieHandler.setDefault(cookieManager);

    HttpCookie cookie = new HttpCookie("lang", "fr");
    cookie.setDomain("twitter.com");
    cookie.setPath("/");
    cookie.setVersion(0);
    cookieManager.getCookieStore().add(new URI("http://twitter.com/"), cookie);

来源: http://developer.android.com/reference/java/净/ HttpURLConnection.html

使用url.openConnection()后,您设置的cookie。

Use url.openConnection() after you set your cookie.

这篇关于Android的调用setRequestProperty在url.openConnection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆