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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值