Java proxy request HTTP (Instance)

Source: Internet
Author: User

Import java. Io. bufferedinputstream;
Import java.net. url;
Import java.net. urlconnection;
Public class testproxyip {
Public static void main (string ARGs []) {
System. getproperties (). setproperty ("HTTP. proxyhost", "58.83.224.217 ");
System. getproperties (). setproperty ("HTTP. proxyport", "8080 ");
System. Out. println (gethtml ("http://iframe.ip138.com/city.asp"); // determines whether the agent is set successfully
}

Private Static string gethtml (string address ){
Stringbuffer html = new stringbuffer ();
String result = NULL;
Try {
URL url = new URL (Address );
Urlconnection conn = URL. openconnection ();
Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; gtb5;. Net CLR 2.0.50727; CBA )");
Bufferedinputstream in = new bufferedinputstream (conn. getinputstream ());
Try {
String inputline;
Byte [] Buf = new byte [1, 4096];
Int bytesread = 0;
While (bytesread> = 0 ){
Inputline = new string (BUF, 0, bytesread, "ISO-8859-1 ");
Html. append (inputline );
Bytesread = in. Read (BUF );
Inputline = NULL;
}
Buf = NULL;
} Finally {
In. Close ();
Conn = NULL;
Url = NULL;
}
Result = new string (html. tostring (). Trim (). getbytes ("ISO-8859-1"), "gb2312"). tolowercase ();
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
Html = NULL;
Return result;
}

}

Running result:

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> your IP address </title>
</Head>
<Body style = "margin: 0px"> <center> your IP address is [58.83.224.217] From: Beijing 21 vianet </center> </body>

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.