WEB开发网
开发学院软件开发Java HttpClient4 Post XML到一个服务器上 阅读

HttpClient4 Post XML到一个服务器上

 2010-08-20 00:00:00 来源:WEB开发网   
核心提示: 2、不指定参数名的方式来POST数据importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.clie

2、不指定参数名的方式来POST数据

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.message.BasicNameValuePair; 
import org.apache.http.entity.*; 

import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
/** 
* 不指定参数名的方式来POST数据 
* 
* @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("<html>你好啊啊</html>", "GBK"); 
                httppost.addHeader("Content-Type", "text/xml"); 
                httppost.setEntity(myEntity); 
                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 String(buff, 0, length)); 
                } 
                httpclient.getConnectionManager().shutdown(); 
        } 
}

Tags:HttpClient Post XML

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接