HttpClient use proxy to access HTTPS "go"

Source: Internet
Author: User

Import Java.io.BufferedReader;

Import Java.io.InputStreamReader;

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.methods.HttpGet;

Import Org.apache.http.conn.params.ConnRoutePNames;

Import org.apache.http.impl.client.DefaultHttpClient;

/**

* HttpClient 4.0 code example for accessing HTTPS via proxy.

*

* @author java Century Network (java2000.net, laozizhu.com)

*/

public class Httpsproxyget {

public static void Main (string[] args) throws Exception {

Defaulthttpclient httpclient = new Defaulthttpclient ();

Certified Data

I am here to write, please fill in the actual situation

Httpclient.getcredentialsprovider (). SetCredentials (New Authscope ("10.60.8.20", 8080),

New Usernamepasswordcredentials ("username", "password"));

Target sites, ports, and protocols to access

Httphost targethost = new Httphost ("www.google.com", 443, "https");

Settings for Agents

Httphost proxy = new Httphost ("10.60.8.20", 8080);

Httpclient.getparams (). Setparameter (Connroutepnames.default_proxy, PROXY);

Destination Address

HttpGet httpget = new HttpGet ("/adsense/login/zh_cn/?");

SYSTEM.OUT.PRINTLN ("Goal:" + targethost);

SYSTEM.OUT.PRINTLN ("Request:" + httpget.getrequestline ());

System.out.println ("Agent:" + proxy);

Perform

HttpResponse response = Httpclient.execute (Targethost, HttpGet);

httpentity entity = response.getentity ();

System.out.println ("----------------------------------------");

System.out.println (Response.getstatusline ());

if (entity!= null) {

System.out.println ("Response Content Length:" + entity.getcontentlength ());

}

Show results

BufferedReader reader = new BufferedReader (New InputStreamReader (Entity.getcontent (), "UTF-8"));

String line = null;

while (line = Reader.readline ())!= null) {

System.out.println (line);

}

if (entity!= null) {

Entity.consumecontent ();

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.