1. obtain data from the Internet:
1. Using the httpurlconnection object, we can obtain webpage data from the network.
URL url = new URL ("http://www.sohu.com ");
Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
Conn. setconnecttimeout (5*1000); // sets the connection timeout.
Conn. setrequestmethod ("get"); // initiate a request in get Mode
If (conn. getresponsecode ()! = 200)
Throw new runtimeexception ("request URL failed ");
Inputstreamis = conn. getinputstream (); // gets the input stream returned by the network.
String result =
Readdata (is, "GBK ");
Conn. Disconnect ();
// The first parameter is the input stream, and the second parameter is character set encoding.
Public static string readdata (inputstream insream, string charsetname) throws exception {
Bytearrayoutputstream outstream = new bytearrayoutputstream ();
Byte [] buffer = new byte [1024];
Int Len = 0;
While (LEN = insream. Read (buffer ))! =-1 ){
Outstream. Write (buffer, 0, Len );
}
Byte [] DATA = outstream. tobytearray ();
Outstream. Close ();
Insream. Close ();
Return newstring (data, charsetname );
2. Using the httpurlconnection object, we can obtain file data from the network.
URL url = new URL ("http://photocdn.sohu.com/20100125/Img269812337.jpg ");
Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
Conn. setconnecttimeout (5*1000 );
Conn. setrequestmethod ("get ");
If (conn. getresponsecode ()! = 200)
Throw new runtimeexception ("request URL failed ");
Inputstreamis = conn. getinputstream ();
Readasfile (is, "img269812337.jpg ");
Public static void readasfile (inputstream insream, file) throws exception {
Fileoutputstream outstream = new fileoutputstream (File );
Byte [] buffer = new byte [1024];
Int Len = 0;
While (LEN = insream. Read (buffer ))! =-1 ){
Outstream. Write (buffer, 0, Len );
}
Outstream. Close ();
Insream. Close ();
}
2. Send request parameters to the Internet
1. Using the httpurlconnection object, we can send request parameters to the network.
String requesturl = "http: // localhost: 8080/itcast/contanctmanage. Do ";
Map <string, string> requestparams = new hashmap <string, string> ();
Requestparams. Put ("Age", "12 ");
Requestparams. Put ("name", "China ");
Stringbuilder Params = new stringbuilder ();
For (Map. Entry <string, string> entry: requestparams. entryset ()){
Params. append (entry. getkey ());
Params. append ("= ");
Params. append (urlencoder. encode (entry. getvalue (), "UTF-8 "));
Params. append ("&");
}
If (Params. Length ()> 0)
Params. deletecharat (Params. Length ()-1 );
Byte [] DATA = Params. tostring (). getbytes ();
URL realurl = new URL (requesturl );
Httpurlconnection conn = (httpurlconnection) realurl. openconnection ();
Conn. setdooutput (true); // The output must be allowed when a POST request is sent.
Conn. setusecaches (false); // No cache is used.
Conn. setrequestmethod ("Post ");
Conn. setrequestproperty ("connection", "keep-alive"); // maintain a persistent connection
Conn. setrequestproperty ("charset", "UTF-8 ");
Conn. setrequestproperty ("Content-Length", String. valueof (data. Length ));
Conn. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded ");
Dataoutputstream outstream = new dataoutputstream (conn. getoutputstream ());
Outstream. Write (data );
Outstream. Flush ();
If (conn. getresponsecode () = 200) {// if the return code is 200, the data returned by the server is obtained.
String result =
Readasstring (conn. getinputstream (), "UTF-8"); // This method is the read method of the custom stream
Outstream. Close ();
System. Out. println (result );
}
2. Send XML data to the Internet
Using the httpurlconnection object, we can send XML data to the network.
Stringbuilder xml = new stringbuilder ();
XML. append ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
XML. append ("<m1v = 10000> ");
XML. append ("<UI = 1 d = \" n73 \ "> China </u> ");
XML. append ("</M1> ");
Byte [] xmlbyte = xml. tostring (). getbytes ("UTF-8 ");
URL url = new URL ("http: // localhost: 8080/itcast/contanctmanage. do? Method = readxml ");
Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
Conn. setconnecttimeout (5*1000 );
Conn. setdooutput (true); // allow output
Conn. setusecaches (false); // No cache is used.
Conn. setrequestmethod ("Post ");
Conn. setrequestproperty ("connection", "keep-alive"); // maintain a persistent connection
Conn. setrequestproperty ("charset", "UTF-8 ");
Conn. setrequestproperty ("Content-Length", String. valueof (xmlbyte. Length ));
Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8 ");
Dataoutputstream outstream = new dataoutputstream (conn. getoutputstream ());
Outstream. Write (xmlbyte); // send XML data
Outstream. Flush ();
If (conn. getresponsecode ()! = 200)
Throw new runtimeexception ("request URL failed ");
Inputstreamis = conn. getinputstream (); // get the returned data
String result = readasstring (is, "UTF-8"); // Method for custom stream reading
Outstream. Close ();