***************************************************
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