HttpPost 请求写入Cookie

DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(getSaveReplyBBSPostsUrl(bbsPostsUrl));

            CookieStore cookieStore = new BasicCookieStore();
            BasicClientCookie cookie = new BasicClientCookie("sessionhash", sessionHash);
            cookie.setPath("/");
            cookie.setDomain("liba.com");
            cookieStore.addCookie(cookie);
            httpClient.setCookieStore(cookieStore);

            MultipartEntity entity = new MultipartEntity();
            entity.addPart("content", new StringBody(content, Charset.forName("UTF-8")));
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == 302) {
                Header[] headers = response.getAllHeaders();
                for (Header header : headers) {
                    if (header.getName().equals("Location")) {
                        log.info("location is:#0", header.getValue());
                        if (header.getValue().contains("/reply_success")) {
                            log.info("reply bbs posts success, bbsPostsUrl:#0, content:#1", bbsPostsUrl, content);
                            return;
                        }
                    }
                }
                log.error("reply bbs posts error, bbsPostsUrl:#0, content:#1, sessionHash:#2", bbsPostsUrl, content, sessionHash);
            }
            log.error("reply bbs posts error, status code:#0", response.getStatusLine().getStatusCode());

你可能感兴趣的:(cookie)