HttpClient4 Post XML to a server

Source: Internet
Author: User

HttpClient4 Post XML to a serverNow the HttpClient introduced on the internet is basically 3. the content of Version x, HttpClient4 API changes a lot compared with version 3. After a simple research on HttpClient4, A HttpClient4 Post XML function is completed. For the post method, the first thing that comes to mind is form submission. post xml naturally comes to mind when defining a variable name, such as xmldata, and then POST the value of this parameter, when the server receives the request, it also uses requset. getParameter ("xmldata") method. What I want to do here is not to use the above method, but not to specify the parameter name for Post. In fact, it is to write a stream into the request. The following are the specific implementation methods: 1. Parameter Name method 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 by specifying the parameter name
*
* @ Author leizhimin 2010-7-8 22:29:28
*/
Public class TestPost {
Public static void main (String [] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient ();
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.exe cute (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 String (buff, 0, length ));
Httpclient. getConnectionManager (). shutdown ();
}
}
} 2. POST Data import org. apache. http. HttpEntity without specifying the parameter name;
Import org. apache. http. HttpResponse;
Import org. apache. http. NameValuePair;
Import org. apache. http. client. HttpClient;
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. entity .*;


Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. util. ArrayList;
Import java. util. List;

/**
* POST data without specifying the parameter name
*
* @ Author leizhimin 2010-7-8 3:22:53
*/
Public class TestPostXml {
Public static void main (String [] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient ();
HttpPost httppost = new HttpPost ("http: // localhost: 8080/waitsrv/GenXmlServlet ");
StringEntity myEntity = new StringEntity ("Httppost. addHeader ("Content-Type", "text/xml ");
Httppost. setEntity (myEntity );
HttpResponse response = httpclient.exe cute (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 String (buff, 0, length ));
}
Httpclient. getConnectionManager (). shutdown ();
}
} Server Receiving Method: package com;

Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. PrintWriter;

/**
* Receive XLM requests
*
* @ Author leizhimin 2010-7-8 1:02:42
*/
Public class GenXmlServlet extends HttpServlet {
Protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// String xml = req. getParameter ("xmldata ");
Resp. setContentType ("text/xml ");
Resp. setCharacterEncoding ("GBK ");
PrintWriter out = resp. getWriter ();
// Out. println (xml );
// System. out. println (xml );
System. out. println ("----------------------");
InputStreamReader reader = new InputStreamReader (req. getInputStream (), "GBK ");
Char [] buff = new char [1024];
Int length = 0;
While (length = reader. read (buff ))! =-1 ){
String x = new String (buff, 0, length );
System. out. println (x );
Out. print (x );
}
}

Protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Resp. setContentType ("text/html ");
PrintWriter out = resp. getWriter ();
Out. println ("Out. println ("Out. println ("<title> Hello World! </Title> ");
Out. println ("Out. println ("<body> ");
Out. println ("Out. println ("</body> ");
Out. println ("}
} Web. xml <? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
Version = "2.5">

<Servlet>
<Servlet-name> GenXmlServlet </servlet-name>
<Servlet-class> com. GenXmlServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> GenXmlServlet </servlet-name>
<Url-pattern>/GenXmlServlet </url-pattern>
</Servlet-mapping>
</Web-app> 3. On the basis of 2, the connection mode is changed to single-threaded reuse import org. apache. http. HttpEntity;
Import org. apache. http. HttpResponse;
Import org. apache. http. NameValuePair;
Import org. apache. http. client. HttpClient;
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. impl. conn. SingleClientConnManager;
Import org. apache. http. message. BasicNameValuePair;
Import org. apache. http. entity .*;


Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. util. ArrayList;
Import java. util. List;

/**
* POST data without specifying the parameter name, and reuse the connection mode in a single thread.
*
* @ Author leizhimin 2010-7-8 3:22:53
*/
Public class TestPostXml2 {
Public static void main (String [] args) throws IOException {
SingleClientConnManager sccm = new SingleClientConnManager ();
HttpClient httpclient = new DefaultHttpClient (sccm );
// HttpGet httpget = new HttpGet (urisToGet [I]);
// HttpClient httpclient = new DefaultHttpClient ();
HttpPost httppost = new HttpPost ("http: // localhost: 8080/waitsrv/GenXmlServlet ");
StringEntity myEntity = new StringEntity ("Httppost. addHeader ("Content-Type", "text/xml ");
Httppost. setEntity (myEntity );
HttpResponse response = httpclient.exe cute (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 String (buff, 0, length ));
}
Httpclient. getConnectionManager (). shutdown ();
}
} Http://hc.apache.org/downloads.cgi

This article is from the "melyan" blog, please be sure to keep this source http://lavasoft.blog.51cto.com/62575/347157

Related Article

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.