HTTPCLIENT4 Post XML to a server

Source: Internet
Author: User

Now the online httpclient is basically full 3.x version of the content, HttpClient4 API changes relative to 3 has changed a lot, HttpClient4 done a simple study, completed a HTTPCLIENT4 post XML function.

For the Post method, the first thought is the form submission, post XML naturally think of is to define a variable name, such as called XMLData, and then POST the value of this parameter, when the server received, naturally also through the Requset.getparameter (" XMLData ") way to receive.

Now what I'm going to do here is not by the way above, but by not specifying the parameter name to post, which is actually writing a stream to the request.

The following are the specific ways to implement:

1, Parameter name way post XML data

import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicNameValuePair;
Import org.apache.http.client.*;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import java.util.*;         
/**
* Post XML
*
* @author leizhimin 2010-7-8 22:29:28
*/
public class Testpost {
by specifying a parameter name public static void Main (string[] args) throws IOException {
HttpClient httpclient = new Defau Lthttpclient ();
HttpPost httppost = new HttpPost ("Http://localhost:8080/waitsrv/GenXmlServlet");
list<namevaluepair> formparams = new arraylist<namevaluepair> ();
Formparams.add New Basicnamevaluepair ("Xmldate", " Formparams.add (New Basicnamevaluepair ("info", "xxxxxxxxx"));
Urlencodedformentity entity = new Urlencodedformentity (formparams, "GBK"); 
//Entity.setcontenttype ("text/xml; CHARSET=GBK ");
Httppost.setentity (entity);
HttpResponse response = Httpclient.execute (HttpPost);
Httpentity resentity = response.getentity ();
InputStreamReader reader = new InputStreamReader (Resentity.getcontent (), "iso-8859-1");
char[] buff = new char[1024];
int length = 0;
while (length = Reader.read (Buff))!=-1) {
System.out.println (new Str ing (buff, 0, length));
Httpclient.getconnectionmanager (). Shutdown ();
}
}
}

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.