Java Socket programming-Get website homepage information

Source: Internet
Author: User

Today, I will write an article every week, from the simplest socket connection to file transmission to protocol implementation,

Remote Desktop control, describes all aspects of Java socket programming and a variety of skills, from common

Socket to Java NIO.

 

This is the first article, which describes how to obtain the home page of a website through Java Socket, including

How to obtain the local machine name and IP address, and how to establish a socket to connect to a remote WEB site

How to Use the http get method to obtain the website homepage content and HTTP Response Headers. Program running result:

 

From this, we can know what language the csdn website is developed based on, what the server is, and what the version is.

For details, refer to the source code:

[Java]
Package com. gloomyfish. socket. tutorial. one;
 
Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java.net. InetAddress;
Import java.net. InetSocketAddress;
Import java.net. Socket;
Import java.net. SocketAddress;
Import java.net. UnknownHostException;
 
Public class HomePageVisitor {
Private Socket client;
Private SocketAddress address;
Public HomePageVisitor (String hostName, int port) throws UnknownHostException, IOException {
Client = new Socket ();
Address = new InetSocketAddress (hostName, port );
}
// GET http: // localhost: 8080/blank/Welcome. do HTTP/1.1/r \ n
Public void printHomePageInfo (String httpString)
{
Try {
// Get local host info
InetAddress inetAddress = InetAddress. getLocalHost ();
System. out. println ("Host Address =" + inetAddress. getHostAddress ());
System. out. println ("Hostname =" + inetAddress. getHostName ());

// Visit the WEBSITE
Client. connect (address );
BufferedWriter bufferedWriter = new BufferedWriter (new OutputStreamWriter (client. getOutputStream (), "UTF8 "));
BufferedWriter. write (httpString );

// -- HTTP/1.1 400 Bad Request if not send \ r \ n
BufferedWriter. write ("\ r \ n ");
BufferedWriter. flush ();
System. out. println ("http request send out ");

BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (client. getInputStream ()));
String content = null;
While (content = bufferedReader. readLine ())! = Null ){
If (getStatusCde (content) = 400 ){
System. err. println ("HTTP/1.1 | HTTP/1.0 400 Bad Request ");
Break;
}
System. out. println ("response contect -->" + content );
}
} Catch (Exception e ){
E. printStackTrace ();
}

}

/**
* <P> try to parse the HTTP response content </p>
* @ Param content
* @ Return status of HTTP
*/
Private static int getStatusCde (String content ){
Int status = 200; // default OK response
If (content = null | "". equals (content ))
Return status;
Else if (! Content. contains ("HTTP/1.1 ")&&! Content. contains ("HTTP/1.0 "))
Return status;
Else if (content. contains ("400") & content. contains ("HTTP/1.1 ")
| Content. contains ("HTTP/1.0 ")){
Status = 400;
}
Return status;
}

Public static void main (String [] args ){
Try {
HomePageVisitor httpVisitor = new HomePageVisitor ("www.csdn.net", 80 );
HttpVisitor. printHomePageInfo ("GET http://www.csdn.net HTTP/1.0 \ r \ n ");
} Catch (UnknownHostException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
If a proxy is used, the HTTP Proxy Address and port are used during Address initialization.
The proxy automatically forwards http get requests. The Code is as follows:

[Java]
Address = new InetSocketAddress (http_proxy, proxy_port)

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.