Java httpurlconnection writing data to a servlet with data flow

Source: Internet
Author: User

***************************************************
Client side code, response request flow
***************************************************


Import java.io.*;
Import java.net.HttpURLConnection;
Import Java.net.URL;


public class Streamposttest {
public static final String encoding_request = "Utf-8";
public static final String encoding_response = "gb2312";


public static void Main (String args[]) {
String requeststring = "We want to send data by stream ...";
HttpURLConnection httpurlconnection;
try {
Establish links
URL gatewayurl = new URL ("Http://localhost/xmlTest.do");
HttpURLConnection = (httpurlconnection) gatewayurl.openconnection ();

Setting connection Properties
Httpurlconnection.setdooutput (TRUE);
Httpurlconnection.setdoinput (TRUE);
Httpurlconnection.setusecaches (FALSE);

To obtain data byte data, the encoding of the request data stream must be consistent with the encoding of the server-side processing request stream below
byte[] requeststringbytes = requeststring.getbytes (encoding_request);

Set request Properties
Httpurlconnection.setrequestproperty ("Content-length", "" "+ requeststringbytes.length);
Httpurlconnection.setrequestproperty ("Content-type", "Application/octet-stream");

Build the output stream and write the data
OutputStream outputstream = Httpurlconnection.getoutputstream ();
Outputstream.write (requeststringbytes);
Outputstream.close ();

Get response status
int responsecode = Httpurlconnection.getresponsecode ();

if (HTTPURLCONNECTION.HTTP_OK = = Responsecode) {
Process data when correctly responding

System.out.println ("");
System.out.println ("Http Response ok ...");
System.out.println ("");

StringBuffer responsebuffer = new StringBuffer ();

String ReadLine;
BufferedReader Responsereader;
Processing the response stream, which must be consistent with the encoding of the server response stream output
Responsereader = new BufferedReader (New InputStreamReader (Httpurlconnection.getinputstream (), ENCODING_RESPONSE));
while ((ReadLine = Responsereader.readline ()) = null) {
Responsebuffer.append (ReadLine). Append ("\ n");
}
Responsereader.close ();

System.out.println ("Http Response:" + responsebuffer);
}

} catch (IOException e) {
E.fillinstacktrace ();
}


}
}


***************************************************
Server segment code, responding to request flow
***************************************************

Get the response stream, get the input object
InputStream InputStream = Httpservletrequest.getinputstream ();
PrintWriter out = Httpservletresponse.getwriter ();

Set up a receive stream buffer to prepare for processing
StringBuffer requestbuffer = new StringBuffer ();
BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream, encoding_request));

System.out.println ("");
System.out.println ("Receive Http Request ...");
System.out.println ("");

Read the stream and convert it into a string
String ReadLine;
while ((ReadLine = Reader.readline ()) = null) {
Requestbuffer.append (ReadLine). Append ("\ n");
}
Reader.close ();

Setting the response encoding
Httpservletresponse.setcharacterencoding (Encoding_response);

Response output
Out.println (Requestbuffer.tostring ());
System.out.println ("Response:" + requestbuffer.tostring ());

Java httpurlconnection writing data to a servlet with data flow

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.