Steps for using httpget and httppost
1. Create an httpget or httppost object and pass in the http get or httppost object through the construction method of the requested URL.
2. Use the defaulthttpclient.exe cute method to send an http get or http post request and return the httpresponse object.
3. Return the response information through the getentity method of the httpresponse interface and perform the correspondingProcessing.
If you use the httppost method to submit an http post request, you also need to use the setentity method of the httppost class to set the request parameters.
How to Use httpurlconnection
1. Use the URL. openconnection method to obtain the httpurlconnection object.
2. Set the request method, such as httpurlconnection. setrequestmethod ("Post ");
3. Set input and output and other permissions.
4. Set the HTTP request header.
5. input and output data.
6. Close the input and output streams.
Send data through socket
Socket socket = new socket ("192.168.17.105", 80 );
Outputstream OS = socket. getoutputstream ();
Outputstreamwriter OSW = new outputstreamwriter (OS );
Bufferedwriter BW = new bufferedwriter (OSW );
Bw. Write ("Hello World ");
Bw. Flush ();
Bw. Close ();
Socket receives data
Socket socket = new socket ("192.168.17.105", 80 );
Inputstream is = socket. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader BR = new bufferedreader (ISR );
String S = "";
While (S = Br. Readline ())! = NULL)
System. Out. println (s );
BR. Close ();
Serversocket listens to client requests
Serversocket = new serversocket (1234); // bind the port
While (true) // code for processing other tasks
{
Socket socket = serversocket. Accept (); // wait for receiving client requests
// Code for processing other tasks
New threadclass (socket). Start ();
}
12. Network Programming