当前位置:首页 > 开发 > 编程语言 > XML > 正文

java发送消息体是xml等格式的http报文

发表于: 2014-08-18   作者:475900947   来源:转载   浏览:
摘要: import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.com
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

//通过java发送http请求
//data可以是xml格式
private static String sendRequest(String url,String data){
        String strResponse;
        //PostMethod method = new PostMethod(url);
        PutMethod method = new PutMethod(url);
        try {
                byte[] b = data.getBytes("UTF-8");
                InputStream is = new ByteArrayInputStream(b,0,b.length);
                RequestEntity re = new InputStreamRequestEntity(is,b.length,"text/plain; charset=utf-8");
                method.setRequestEntity(re);
                method.setRequestHeader("Content-Type","text/plain;charset=utf-8");
                HttpClient client = new HttpClient();
                int statusCode = client.executeMethod(method);
                if(statusCode == 200){
                        strResponse = new String(method.getResponseBodyAsString().getBytes(),"UTF-8");
                }else{
                        strResponse = "error1"+statusCode+".";
                }
                method.releaseConnection();
        } catch (Exception e) {
                e.printStackTrace();
                strResponse = "error2"+e.getMessage();
        }
        return strResponse;
}

java发送消息体是xml等格式的http报文

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号