读书人

JAVA POST形式发送XML

发布时间: 2012-12-26 14:39:29 作者: rapoo

JAVA POST方式发送XML
public static final String POST_URL = "http://www.jiucool.com/api";
public static void poststh(String content){//content为你要拼写的XML文档字符串
URL url = null ;

try {
url = new URL(POST_URL);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
URLConnection uc = null;
OutputStreamWriter out = null;
BufferedReader rd = null;
try {
uc = url.openConnection();
uc.setDoOutput(true); //....必须设置为'true'.
uc.setRequestProperty("Content-Type", "text/xml"); //记住这行不能少否则会出错
out = new OutputStreamWriter(uc.getOutputStream(),"utf-8");
out.write(content);
out.flush();
out.close();

rd = new BufferedReader(new InputStreamReader(uc.
getInputStream(),"utf-8"));
String responseStr;
while ( (responseStr = rd.readLine()) != null) {
System.out.println("发送结果=" + responseStr);
}

rd.close();
} catch (IOException e) {

e.printStackTrace();
}

}
1 楼 wxiaoneng 2011-10-18 henhao

读书人网 >操作系统

热点推荐