Set the proxy for httpclient and set the HTTP header.

Source: Internet
Author: User
Tags http request
In the school Park network, to agents to the Internet. What to do when writing a program to get online resources. You can also set up a proxy for your program. I use the 3.1 version of HttpClient. Some websites can be accessed by the browser, but the program can be browser-like, mainly set HTTP headers.

Set in Hostconfiguration.setproxy. such as the following code: package com.chenlb;      import java.io.ioexception;   import  java.util.arraylist;   import java.util.list;      import  org.apache.commons.httpclient.defaulthttpmethodretryhandler;   import  org.apache.commons.httpclient.header;   import org.apache.commons.httpclient.httpclient;    import org.apache.commons.httpclient.httpexception;   import  org.apache.commons.httpclient.httpstatus;   import  org.apache.commons.httpclient.usernamepasswordcredentials;   import  org.apache.commons.httpclient.auth.authscope;   import  org.apache.commons.httpclient.methods.getmethod;   import  org.apache.commons.httpclient.params.httpmethodparams;      public class  httpclientuse {          PUBLIC&NBSp;static void main (String[] args)  throws httpexception, ioexception {            HttpClient httpClient = new  HttpClient ();               Httpclient.gethostconfiguration (). SetProxy ("localhost",  808);               /*//Need verification            Usernamepasswordcredentials creds = new usernamepasswordcredentials ("Chenlb",  "123456 ");            httpclient.getstate (). Setproxycredentials ( authscope.any, creds);          */              //Set HTTP headers             list

If you want the username and password to be verified, please remove the/* */comment. Make validation valid.

Verification, the key is: usernamepasswordcredentials creds = new Usernamepasswordcredentials ("Chenlb", "123456"); Httpclient.getstate (). Setproxycredentials (Authscope.any, creds);

Sets the HTTP request header.   list

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.