Java: Simple HTTPUrlConnection example

来源:互联网 发布:淘客软件是什么 编辑:程序博客网 时间:2024/06/06 20:01

Java:Simple HTTPUrlConnection example

 

package ChinaCache;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;

 

import java.net.MalformedURLException;

import java.net.ProtocolException;

import java.net.URL;

 

public class SimpleHTTPRequest {

/**                                                                                                                                                    */

public static void main(String[]args) {

       HttpURLConnectionconnection = null;

       OutputStreamWriter wr= null;

       BufferedReader rd = null;

       StringBuilder sb = new StringBuilder();;

       String line = null;

       URL serverAddress = null;

 

       try {

            serverAddress =new URL("http://www.globalsources.com/");

            //1) open connection

            connection =(HttpURLConnection) serverAddress.openConnection();

            //2) connection settings

            connection.setRequestMethod("GET");

            connection.setDoOutput(true);

            connection.setReadTimeout(10000);

//3)connect

            connection.connect();

            //4)write data

            // get theoutput stream writer and write the output to the server

            // not neededin this example

            // wr = newOutputStreamWriter(connection.getOutputStream());

           // wr.write("....");

            // wr.flush();

           

            //5)read the result from the server

            rd  = new BufferedReader(newInputStreamReader(connection.getInputStream()));

            while ((line =rd.readLine()) != null) {

                 sb.append(line+ '/n');

            }

            System.out.println(sb.toString());

            //6)close

            rd.close();

           

       } catch(MalformedURLException e) {

            e.printStackTrace();

       } catch(ProtocolException e) {

            e.printStackTrace();

       } catch (IOException e){

            e.printStackTrace();

       } finally {

       }

}

}

 

Notes:

1.StringBuilder:

Amutable sequence of characters. This classprovides an API compatible with StringBuffer, but with noguarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by asingle thread (as is generally the case). Wherepossible, it is recommended that this class be used in preference to StringBuffer as it willbe faster under most implementations.

2. HttpURLConnection对象参数问题

1) 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;
httpUrlConnection.setDoOutput(true);

 

2) 设置是否从httpUrlConnection读入,默认情况下是true;
httpUrlConnection.setDoInput(true);

 

3) Post 请求不能使用缓存
httpUrlConnection.setUseCaches(false);

 

4) 设定传送的内容类型是可序列化的java对象(如果不设此项,在传送序列化对象时,WEB服务默认的不是这种类型时可能抛java.io.EOFException)
httpUrlConnection.setRequestProperty("Content-type","application/x-java-serialized-object");

 

5) 设定请求的方法为"POST",默认是GET
httpUrlConnection.setRequestMethod("POST");

 

6) 1)-5) 必须在url.openConnection()之后和httpUrlConnection.connect()之前完成

 

3. HttpURLConnection写数据与发送数据问题:

1) 现在通过输出流对象构建对象输出流对象,以实现输出可序列化的对象。
   ObjectOutputStream objOutputStrm = newObjectOutputStream(outStrm);

 

2) 向对象输出流写出数据,这些数据将存到内存缓冲区中
   objOutputStrm.writeObject(newString("
我是测试数据"));

 

3) 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream
   objOutputStm.flush();

 

4) 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中, 在调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器
   objOutputStm.close();

5) 调用HttpURLConnection连接对象的getInputStream()函数,将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
  InputStream inStrm =httpConn.getInputStream(); // <===
注意,实际发送请求的代码段就在这里

  上边的httpConn.getInputStream()方法已调用,本次HTTP请求已结束,下边向对象输出流的输出已无意义,既使对象输出流没有调用close()方法,下边的操作也不会向对象输出流写入任何数据. 因此,要重新发送数据时需要重新创建连接、重新设参数、重新创建流对象、重新写数据、
6)
重新发送数据(至于是否不用重新这些操作需要再研究)
   objOutputStm.writeObject(newString(""));
   httpConn.getInputStream();

 

4URL请求的类别:分为二类,GETPOST请求。二者的区别在于:
      a:) get
请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet
      b:) post
get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。

5.其他:

  a:) HttpURLConnectionconnect()函数,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求
   
无论是post还是gethttp请求实际上直到HttpURLConnectiongetInputStream()这个函数里面才正式发送出去


   b:)
在用POST方式发送URL请求时,URL请求参数的设定顺序是重中之重,对connection对象的一切配置(那一堆set函数)都必须要在connect()函数执行之前完成。而对outputStream的写操作,又必须要在inputStream的读操作之前。
   
这些顺序实际上是由http请求的格式决定的。
   
如果inputStream读操作在outputStream的写操作之前,会抛出例外:
    java.net.ProtocolException: Cannot write output afterreading input.......
      
   c:) http
请求实际上由两部分组成,一个是http头,所有关于此次http请求的配置都在http头里面定义,一个是正文content
    connect()
函数会根据HttpURLConnection对象的配置值生成http头部信息,因此在调用connect函数之前,
   
就必须把所有的配置准备好。


   d:)
http头后面紧跟着的是http请求的正文,正文的内容是通过outputStream流写入的,
   
实际上outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,
   
而是存在于内存缓冲区中,待outputStream流关闭时,根据输入的内容生成http正文。
   
至此,http请求的东西已经全部准备就绪。在getInputStream()函数调用的时候,就会把准备好的http请求
   
正式发送到服务器了,然后返回一个输入流,用于读取服务器对于此次http请求的返回信息。由于http
   
请求在getInputStream的时候已经发送出去了(包括http头和正文),因此在getInputStream()函数
   
之后对connection对象进行设置(对http头的信息进行修改)或者写入outputStream(对正文进行修改)
   
都是没有意义的了,执行这些操作会导致异常的发生。

4 Servlet端的开发注意点:
a:)
对于客户端发送的POST类型的HTTP请求,Servlet必须实现doPost方法,而不能用doGet方法。
b:)
HttpServletRequestgetInputStream()方法取得InputStream的对象,比如:
     InputStream inStream = httpRequest.getInputStream();
    
现在调用inStream.available()(该方法用于返回此输入流下一个方法调用可以不受阻塞地
    
从此输入流读取(或跳过)的估计字节数)时,永远都反回0。试图使用此方法的返回值分配缓冲区,
    
以保存此流所有数据的做法是不正确的。那么,现在的解决办法是
     Servlet
这一端用如下实现:
     InputStream inStream = httpRequest.getInputStream();
     ObjectInputStream objInStream = newObjectInputStream(inStream);
     Object obj = objInStream.readObject();
     //
做后续的处理
     //
。。。。。。
     //
。。。 。。。
    
而客户端,无论是否发送实际数据都要写入一个对象(那怕这个对象不用),如:
     ObjectOutputStream objOutputStrm = newObjectOutputStream(outStrm);
     objOutputStrm.writeObject(new String(""));//
这里发送一个空数据
     //
甚至可以发一个null对象,服务端取到后再做判断处理。
     objOutputStrm.writeObject(null);
     objOutputStrm.flush();
     objOutputStrm.close();

注意:上述在创建对象输出流ObjectOutputStream,如果将从HttpServletRequest取得的输入流
      (
:newObjectOutputStream(outStrm)中的outStrm)包装在BufferedOutputStream流里面,
     
则必须有objOutputStrm.flush();这一句,以便将流信息刷入缓冲输出流.如下:
      ObjectOutputStream objOutputStrm = newObjectOutputStream(new BufferedOutputStream(outStrm));
      objOutputStrm.writeObject(null);
      objOutputStrm.flush(); // <======
此处必须要有.
      objOutputStrm.close();

 


       

========= Another article about  URLConnection ==========

Reading from and Writing to a URLConnection

The URLConnectionclass contains many methods that let you communicate with the URL over thenetwork. URLConnection is anHTTP-centric class; that is, many of its methods are useful only when you areworking with HTTP URLs. However, most URL protocols allow you to read from andwrite to the connection. This section describes both functions.

Reading from a URLConnection

The following program performs the same function as the URLReader program shown in ReadingDirectly from a URL.

However, rather than getting an input stream directly from the URL, thisprogram explicitly retrieves a URLConnectionobject and gets an input stream from the connection. The connection is openedimplicitly by calling getInputStream.Then, like URLReader, thisprogram creates a BufferedReaderon the input stream and reads from it. The bold statements highlight thedifferences between this example and the previous

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");

        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));

        String inputLine;

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}

The output from this program is identical to the output fromthe program that opens a stream directly from the URL. You can use either wayto read from a URL. However, reading from a URLConnectioninstead of reading directly from a URL might be more useful. This is becauseyou can use the URLConnectionobject for other tasks (like writing to the URL) at the same time.

Again, if the program hangs or you see an error message, you may have to setthe proxy host so that the program can find the Yahoo server.

Writing to a URLConnection

Many HTML pages contain forms-- text fields and otherGUI objects that let you enter data to send to the server. After you type inthe required information and initiate the query by clicking a button, your Webbrowser writes the data to the URL over the network. At the other end theserver receives the data, processes it, and then sends you a response, usuallyin the form of a new HTML page.

Many of these HTML forms use the HTTP POST METHOD to send data to theserver. Thus writing to a URL is often called posting to a URL. Theserver recognizes the POST request and reads the data sent from the client.

For a Java program to interact with a server-side process it simply must beable to write to a URL, thus providing data to the server. It can do this byfollowing these steps:

  1. Create a URL.
  2. Retrieve the URLConnection object.
  3. Set output capability on the URLConnection.
  4. Open a connection to the resource.
  5. Get an output stream from the connection.
  6. Write to the output stream.
  7. Close the output stream.

Here is a small servletnamed ReverseServlet( or if you prefer a cgi-binscript ). You can use this servlet to test the following example program.

The servlet running in a container reads from its InputStream, reverses thestring, and writes it to its OutputStream. The servlet requires input of theform string=string_to_reverse,where string_to_reverse isthe string whose characters you want displayed in reverse order.

Here's an example program that runs the ReverseServletover the network through a URLConnection:

import java.io.*;
import java.net.*;

public class Reverse {
    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            System.err.println("Usage:  java Reverse " +

                               "http://<location of your servlet/script>" + 
                               " string_to_reverse");
            System.exit(1);
        }

        String stringToReverse = URLEncoder.encode(args[1], "UTF-8");


        URL url = new URL(args[0]);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(
                              connection.getOutputStream());

        out.write("string=" + stringToReverse);
        out.close();

        BufferedReader in = new BufferedReader(
                               new InputStreamReader(
                               connection.getInputStream()));
                              
        String decodedString;


        while ((decodedString = in.readLine()) != null) {
            System.out.println(decodedString);
        }
        in.close();
    }
}

Let's examine the program and see how it works. First, theprogram processes its command-line arguments:

if (args.length != 2) {
    System.err.println("Usage:  java Reverse " +
                        "http://<location of your servlet/script>" +
                        " string_to_reverse");

    System.exit(1);
}      

String stringToReverse = URLEncoder.encode(args[1], "UTF-8");

These statements ensure that the user provides two and onlytwo command-line arguments to the program. The command-line arguments are thelocation of the ReverseServletand the string that will be reversed. It may contain spaces or othernon-alphanumeric characters. These characters must be encoded because thestring is processed on its way to the server. The URLEncoder class methods encode the characters.

Next, the program creates the URLobject, and sets the connection so that it can write to it:

URL url = new URL(args[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

The program then creates an output stream on the connectionand opens an OutputStreamWriteron it:

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

If the URL does not support output, getOutputStream method throws an UnknownServiceException. If the URL doessupport output, then this method returns an output stream that is connected tothe input stream of the URL on the server side--the client's output is theserver's input.

Next, the program writes the required information to the output stream andcloses the stream:

out.write("string=" + stringToReverse);
out.close();

This code writes to the output stream using the write method. So you can see thatwriting data to a URL is as easy as writing data to a stream. The data writtento the output stream on the client side is the input for the servlet on theserver side. The Reverseprogram constructs the input in the form required by the script by prepending string= to the encoded string to bereversed.

The serlvet reads the information you write, performs a reverse operation onthe string value, and then sends this back to you. You now need to read thestring the server has sent back. The Reverseprogram does it like this:

BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    connection.getInputStream()));
                              
String decodedString;

while ((decodedString = in.readLine()) != null) {

    System.out.println(decodedString);
}
in.close();

If your ReverseServletis located at http://foobar.com/servlet/ReverseServlet,then when you run the Reverseprogram using

http://foobar.com/servlet/ReverseServlet "Reverse Me"

as the argument (including the double quote marks), youshould see this output:

Reverse Me
 reversed is:
eM esreveR

 

原创粉丝点击