About transferring XML data __java via JAVA HTTP POST requests

Source: Internet
Author: User
Tags http post domain transfer stringbuffer
As for the cross-domain transfer of XML data, many problems have been encountered due to the new contact. Here a number of related methods are recorded, but also to provide you with some reference (novice application, the master please Hey hehe).
Client: Requests and receives return data through an HTTP POST method. (Used to request a person's interface and receive the returned data)
Server: Receives data from HTTP POST requests and returns data. (for others to request their own interface, receive request data and return data)
Here is the approximate implementation code:
Client:
public void Test () {
try {
HttpServletResponse response = Servletactioncontext.getresponse ();
Sets the encoding type of the returned data
Response.setcharacterencoding ("GBK");
String xml = "-<-?-xml-version=-" 1.0-"-encoding=-" gb2312-"-?->" +
"-<-ddd->-"
Inside some of the XML data, the format itself changed, was filtered to add something
"-<-/ddd->-";

String URL = "Request Address";
HttpClient client = new HttpClient ();
Set proxy server addresses and ports
Client.gethostconfiguration (). SetProxy ("Proxy_host_addr", Proxy_port);
Using the Get method, if the server needs to be connected via HTTPS, it only needs to replace HTTP in the URL below with HTTPS
HttpMethod method = new GetMethod ("http://java.sun.com");
Using the Post method
Postmethod post = new Postmethod (URL);
Set the XML data to send the request, where you can set the parameters without sending the XML data directly
Post.setparameter (key, value);
Post.setrequestentity (new Stringrequestentity (XML, "Text/xml", "GBK"));
Execute request
Client.executemethod (POST);
Print the returned information
byte[] by = Post.getresponsebody ();
PrintWriter pw = Response.getwriter ();
Pw.print (A new String (by));
Release connection
Post.releaseconnection ();
catch (Exception e) {
E.printstacktrace ();
}
}


Server:
Here is the key part of the code
------------receive the XML data from the Logistics party request-----------------------
Gets the length of the stream that the request receives, because if the Struts2 frame is used here, when the sender puts the Content-type
setting to application/x-www-form-urlencoded will result in filtered streaming of data.
If Len is not-1, and the following BR is empty, the instructions are filtered out.
int len = Request.getcontentlength ();
SYSTEM.OUT.PRINTLN ("Data Flow length:" +len);
Gets the input stream for the HTTP request
InputStream is = Request.getinputstream ();
An HTTP request input stream was established to create a BufferedReader object
BufferedReader br = new BufferedReader (new InputStreamReader (IS, "UTF-8"));
BufferedReader br = Request.getreader ();

Read HTTP request content
String buffer = null;
StringBuffer sb = new StringBuffer ();

while (buffer = Br.readline ())!= null) {
Display the request parameters read to in the page
Sb.append (buffer);
}
On the solution: Request.getinputstream or Request.getreader () is filtered out of the problem, you can use JSP to receive data, and then the Content-type set to xml/ The text type again requests the real interface to receive the data. This is how I think of solutions, if a friend has a better way, please leave a message to give a hint, I also go to research.

Although the content of this chapter is very simple, but for beginners of us, a problem may be burned, we are interested can be recorded, hehe

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.