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 ();
}
}
}