Java- apache http client- usage examples showing use of cookies and extracting response from HTTPResponse object

▼魔方 西西 提交于 2019-11-30 14:19:13

1)AS for cookies,see that exapmle:

httpcomponents-client-4.1.3\examples\org\apache\http\examples\client\ClientCustomContext.java

main code:

HttpClient httpclient = new DefaultHttpClient();
        try {
            // Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();

            // Create local HTTP context
            HttpContext localContext = new BasicHttpContext();
            // Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            HttpGet httpget = new HttpGet("http://www.google.com/");

            System.out.println("executing request " + httpget.getURI());

            // Pass local context as a parameter
            HttpResponse response = httpclient.execute(httpget, localContext);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }

2)You can get all you need from response and:

HttpEntity entity = response.getEntity();
entity.getContent()

Just read the examples in: httpcomponents-client-4.1.3\examples\org\apache\http\examples\client of httpcomponents-client-4.1.3-bin.zip which is downloaded from its website.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!