hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jens Kübler (JIRA) <j...@apache.org>
Subject [jira] [Created] (HTTPCLIENT-1794) Cookies not forwarded through proxies
Date Wed, 07 Dec 2016 13:07:58 GMT
Jens Kübler created HTTPCLIENT-1794:
---------------------------------------

             Summary: Cookies not forwarded through proxies
                 Key: HTTPCLIENT-1794
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1794
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: HttpClient (classic)
    Affects Versions: 4.5.2
         Environment: win32_64
            Reporter: Jens Kübler


Cookies are not being sent through proxies.
See  the following code. This does only work when the commented line is being set.

package vi.java;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;

public class ProxyCookieExample {

	public static void main(String args[]) throws ClientProtocolException, IOException {
		CookieStore cookieStore = new BasicCookieStore();
		// Populate cookies if needed
		BasicClientCookie cookie = new BasicClientCookie("oraclelicense", "accept-securebackup-cookie");
		cookie.setDomain(".oracle.com");
		cookie.setPath("/");
		cookieStore.addCookie(cookie);
		
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(
				new AuthScope("myproxy", 8080),
				new UsernamePasswordCredentials("user", "password"));
		
		HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).setDefaultCredentialsProvider(credsProvider).build();
		HttpHost proxy = new HttpHost("myproxy", 8080);
		
		RequestConfig config = RequestConfig.custom()
                .setProxy(proxy)
                .setCookieSpec(CookieSpecs.DEFAULT)
                .build();
		
		HttpGet httpGet = new HttpGet("http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-windows-i586.exe");
//		httpGet.addHeader("Cookie","oraclelicense=accept-securebackup-cookie");
		httpGet.setConfig(config);
		
		HttpResponse response1 = client.execute(httpGet);
		
	    HttpEntity entity1 = response1.getEntity();
	    InputStream content = entity1.getContent();
	    try(FileOutputStream fileOutputStream = new FileOutputStream("jdk-8u112-windows-i586.exe"))
{
	    	byte[] buffer = new byte[16384]; // Adjust if you want
	    	int bytesRead;
	    	while ((bytesRead = content.read(buffer)) != -1)
	    	{
	    		fileOutputStream.write(buffer, 0, bytesRead);
	    	}		    
	    }
	}
	
}




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message