Sending Cookie info in HttpRequest

前端 未结 5 1508
抹茶落季
抹茶落季 2020-12-10 20:11

I want to call a web service that requires an authentication cookie.

I have the cookie name and value. but I don\'t know how to inject the cookie in the request.

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 20:43

    HttpClient httpClient = new DefaultHttpClient();
    
    CookieStore cookieStore = new BasicCookieStore();
    Cookie cookie = new BasicClientCookie("name", "value");
    cookieStore.addCookie(cookie);
    
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    
    HttpGet httpGet = new HttpGet("http://www.domain.com/"); 
    
    HttpResponse response = httpClient.execute(httpGet, localContext);
    

提交回复
热议问题