Apply httpclient to a variety of stubborn Web servers

Source: Internet
Author: User

In general, we use IE or navigator browser to access a Web server, to browse the page to view the information or submit some data and so on. Some of the pages visited are just plain pages, some need to be logged in before they can be used, or they need to be authenticated and are transmitted via encryption, such as HTTPS. Currently, the browsers we use to handle these situations do not pose a problem. However, you may have to use a program to access some of these pages at some point, such as "steal" some data from someone else's Web page, and perform a function by using a page from some site, such as saying we want to know where a phone number belongs and we don't have the data So we have to use other companies already have the site to complete this function, this time we need to submit the phone number to the Web page and from the returned page to parse out the data we want to. If the other side is just a very simple page, then our program will be very simple, this article also does not need to be a big waste of breath here. However, given some of the service licensing issues, many companies provide pages are often not a simple URL can be accessed, but must be registered and then login to use the service provided by the page, this time it involves the processing of cookie problems. We know that the current popular dynamic web technologies such as ASP, JSP are all through cookies to process session information. In order to enable our program to use the services provided by others, we require that the program first log in and then access the service page, the process will need to handle cookies, think about how scary it is when you use java.net.HttpURLConnection to accomplish these functions! And this is just what we call a stubborn Web server in a very common "stubborn"! Like uploading files via HTTP? No need for headaches, these problems with "it" is easy to solve!

We cannot enumerate all the possible stubbornness, and we will deal with several of the most common problems. Of course, as I said earlier, It's scary if we use java.net.HttpURLConnection to fix these problems ourselves, so before we get started, let's start with an open source project, the httpclient in the Apache Open source organization, which is subordinate to the Jakarta Common S project, the current version is 2.0RC2. Commons there would have been a net subproject, but also put the httpclient separately, visible HTTP server access is not easy.

The Commons-httpclient project is designed to simplify the communication programming of HTTP clients and servers. Through it can make the original very headache things now easy to solve, for example, you are no longer the way HTTP or HTTPS communication, tell it you want to use HTTPS, the rest of the matter to httpclient for you to complete. This article will be for us to write HTTP client program frequently encountered several problems to explain how to use httpclient to solve them, in order to let the reader more quickly familiar with the project we first give a simple example to read the content of a Web page, And then gradually solve all the problems in advance.

1. Read Web page (HTTP/HTTPS) content

Here's a simple example of how to access a page

/*
* Created on 2003-12-14 by Liudong
*/

Package http.demo;
Import java.io.IOException;
Import org.apache.commons.httpclient.*;
Import org.apache.commons.httpclient.methods.*;

/**
* Simplest HTTP client to demonstrate access to a page via GET or post
* @authorLiudong
*/

public class SimpleClient { public static void Main (string[] args) throws IOException
{
HttpClient client = new HttpClient ();
   Set proxy server address and port

//client.gethostconfiguration (). SetProxy ("Proxy_host_addr", proxy_port);
With the Get method, if the server needs to connect over HTTPS, it only needs to replace HTTP in the following URL with HTTPS
Httpmethodmethod=newgetmethod ("http://java.sun.com" );    
//Using Post methods
//httpmethod method = new Postmethod ("http://java.sun.com");
Client.executemethod (method);    
//print server return status
System.out.println (Method.getstatusline ());
   Prints the returned information
System.out.println (method.getresponsebodyasstring ());
Release the connection
Method.releaseconnectioN ();
}
}

In this example, you first create an instance of HTTP client (httpclient) and then choose to submit the method either get or post, and finally execute the commit method on the HttpClient instance, and then read the server feedback from the selected submission method. This is the basic process of using httpclient. In fact, a line of code can also handle the entire request process, very simple!

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.