Mad*_*han 1 cookies android json
我正在尝试从 url 解析 json 数据,当我尝试创建连接时,它抛出异常 java.net.ProtocolException: cannot write request body after response has been read
我收到的响应消息为Not found。
我在网络浏览器中检查了 url,当我用我的凭据登录时,它显示了 Json 数据。
所以,我发现我需要将 cookie 添加到我的连接中,但我不知道该怎么做。
public void parseData(String cookie){
HttpUrlConnection connection;
try{
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Cookie", cookie);
Log.e(TAG, "cookie " + cookie);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
Log.e(TAG,connection.getResponseMessage());
/**
here i'm trying to parse the data
using BufferedReader calss
**/
}
catch(IOException e){}
}
Run Code Online (Sandbox Code Playgroud)
我需要在连接中添加 cookie。请帮我解决这个问题。
根据此链接, 您可以执行以下操作:
必须在调用 connect 方法之前设置值:
URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp");
URLConnection urlConn = myUrl.openConnection();
Run Code Online (Sandbox Code Playgroud)
创建一个 cookie 字符串:
String myCookie = "userId=igbrown";
Run Code Online (Sandbox Code Playgroud)
将 cookie 添加到请求中:使用 setRequestProperty(String name, String value); 方法,我们将添加一个名为“Cookie”的属性,将上一步中创建的 cookie 字符串作为属性值传递。
urlConn.setRequestProperty("Cookie", myCookie);
Run Code Online (Sandbox Code Playgroud)
将 cookie 发送到服务器:要发送 cookie,只需在我们添加了 cookie 属性的 URLConnection 上调用 connect():
urlConn.connect()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21752 次 |
| 最近记录: |