java - Add HttpCookie to CookieStore -
i need add few cookies authorize on website. cookies added successfully, missing when making request:
import java.io.*; import java.net.*; public class main { static public void main(string[] args) throws exception { cookiemanager cookiemanager = new cookiemanager(null, cookiepolicy.accept_all); cookiestore cookiejar = cookiemanager.getcookiestore(); cookiehandler.setdefault(cookiemanager); httpcookie cookie = new httpcookie("name123", "value123"); cookiejar.add(new uri("http://httpbin.org"), cookie); httpurlconnection connection = (httpurlconnection) new url("http://httpbin.org/cookies").openconnection(); connection.setrequestmethod("get"); connection.connect(); bufferedreader in; stringbuilder response = new stringbuilder(); string inputline; in = new bufferedreader(new inputstreamreader(connection.getinputstream())); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close(); system.out.println(response.tostring()); } }
but empty map of cookies in response
{ "cookies": {}}
please, tell me should fix it.
cookie.setpath( "/" ); cookie.setversion( 0 );
do trick ¯\_(ツ)_/¯
Comments
Post a Comment