POST发送XML数据

package com.itheima.xml;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpPostTest {

	void testPost(String urlStr) {
		OutputStreamWriter out=null;  
	    BufferedReader in=null;  
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);  
            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");

            out = new OutputStreamWriter(con.getOutputStream());    
            String xmlInfo = getXmlInfo();
            System.out.println("urlStr=" + urlStr);
            System.out.println("xmlInfo=" + xmlInfo);
            out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
            out.flush();
            out.close();
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            for (line = in.readLine(); line != null; line = in.readLine()) {
                System.out.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{  
            try {  
                if(out!=null)  
                    out.close();  
                if(in!=null)  
                    in.close();  
            } catch (IOException ex) {  
                ex.printStackTrace();  
            }  
        }  
	}
    private String getXmlInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("<order><id>164228692</id>");
        sb.append("    <user>");
        sb.append("        <channelcode>51bi</channelcode>");
        sb.append("        <trackingcode>32151656</trackingcode>");
        sb.append("    </user><product><deal>5081435</deal></product>");
        sb.append("    <order_time>2013-10-15 10:24:00</order_time>");
        sb.append("        <pay_time>2013-10-15 10:27:00</pay_time>");
        sb.append("           <status>ORDER</status>");
        sb.append("        <number>2</number>");
        sb.append("        <cost><total_amount>199</total_amount></cost>");
        sb.append("</order>");
        return sb.toString();
    }
public static void method1(){
		try {
			//http://58.221.72.184:5868
			// http地址          
			String httpUrl = "http://192.168.0.244:8080/bjsearch/dianpingOrderBack.jhtml";          
			//HttpPost连接对象      
			DefaultHttpClient httpclient = new DefaultHttpClient(); 
	        HttpPost httppost = new HttpPost(httpUrl); 
	        StringEntity myEntity = new StringEntity(getXmlInfo(), "UTF-8");
	        httppost.setEntity(myEntity);
	       // httppost.addHeader("Content-Type", "text/xml");
	        
	        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
	        httppost.addHeader("Accept", "*/*");
	       // httppost.setEntity(new StringEntity(content));
            
           // HttpResponse response = httpClient.execute(httpPost);
	        
	        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();   
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		 String url = "http://192.168.0.88:8181/bjsearch/dianpingOrderBack.jhtml";
	     new HttpPostTest().testPost(url);
	}

}

你可能感兴趣的:(xml,post)