在Httpurlconnection请求中设置cookie

时间:2015-09-24 03:20:31

标签: android session-cookies httpurlconnection

我想从我的HTTPRequest标头中的先前HTTPResponse设置一个cookie以进行身份​​验证。

我将我的cookie与JSONObject以及相应的URL一起保存在StringArray中,以便进行测试并将其传输到AsyncTask。

后来我可以用cookiestore,sharedpreferences实现它......但是现在我的cookie保存在postData [2]中。

protected JSONObject doInBackground(String... postData)
{
    try 
    {

        URL url=new URL(String.valueOf(postData[1]));
        HttpsURLConnection httpsConnection (HttpsURLConnection)url.openConnection();

        httpsConnection.setRequestMethod("POST");
        httpsConnection.setRequestProperty("Accept", "application/json");
        httpsConnection.setRequestProperty("content-type", "application/json");

        httpsConnection.setRequestProperty("cookie",postData[2]);

        httpsConnection.setInstanceFollowRedirects(false);
        httpsConnection.setDoInput(true);
        httpsConnection.setDoOutput(true);
        httpsConnection.setUseCaches(false);


        postOut=new DataOutputStream(httpsConnection.getOutputStream());
        postOut.writeBytes(String.valueOf(postData[0]));
        postOut.flush();
        postOut.close();
    //.....
    //.....
    }
    catch (Exception e)
    {}
}

错误消息是 errors“:[{”value“:”“,”context“:”“,”text“:”Session is invalid。“,”contextPath“:”“,”code“:10101,”details“:[], “contextObject”: “”}]

如果我和邮递员一起尝试这一切都很好,所以我的想法是cookie没有正确设置?

0 个答案:

没有答案
相关问题