如何在POST方法中格式化Cookie以匹配iOS

时间:2015-01-06 19:40:29

标签: android ios cookies httpurlconnection

尝试将此iOS代码复制到我的Android httpUrlConnection post方法,下面是objc:

NSDictionary *properties = @{
                             NSHTTPCookiePath: @"/",
                             NSHTTPCookieDomain: [request.URL host],
                             NSHTTPCookieName: @"session_state",
                             NSHTTPCookieValue: state,
                             };

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
NSDictionary *cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:@[cookie]];

以下是我在Android应用中设置的内容:

conn.setRequestProperty("Cookie", "session_state=" + state);

我的问题是如何正确匹配这两个,以便我的Android cookie等同于iOS示例

1 个答案:

答案 0 :(得分:0)

为了回答我自己的问题,我使用httpClient和httpPost代替httpUrlConnection,它看起来像这样:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Cookie", "session_state=" + state);