`
wxiaoneng
  • 浏览: 8146 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

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

相关推荐

Global site tag (gtag.js) - Google Analytics