12. Network Programming

Source: Internet
Author: User

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

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.