eagle

学无止境,细节决定成败.
posts - 12, comments - 11, trackbacks - 0, articles - 2
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

httpclient 实现java后台post xml请求

Posted on 2012-01-30 14:22 月下孤城 阅读(6025) 评论(1)  编辑  收藏
apache的httpClient工具包能实现http相关请求。以下是一个java后台包装xml参数请求服务的工具类。
/**
     * Post Http request with XML parameter
     * 
@param requestUrl 
     * 
@param xmlData    request xml parameter
     * 
@param contentType such as "application/xml"
     * 
@param charset such as "UTF-8" or "GBK"
     * 
@return
     
*/

    
public static HttpRespondResult postXmlRequest(String requestUrl, String xmlData, String contentType, String charset){
        HttpRespondResult revObj 
= new HttpRespondResult();
        
//init PostMethod object.
        PostMethod post = new PostMethod(requestUrl);
        
        
try {
            
//wrape the request entity.
            RequestEntity requestEntity = new StringRequestEntity(xmlData, contentType, charset);
            post.setRequestEntity(requestEntity);
            HttpClient httpClient 
= new HttpClient();
            
// send the post http request and reture status code.
            int statusCode = httpClient.executeMethod(post);
            
// get reture content from server side.
            String bodyContent = post.getResponseBodyAsString();
            
            
// populate the reture values to vo.
            revObj.setStatusCode(statusCode);
            revObj.setRespondbodyContent(bodyContent);
            
        }
 catch (UnsupportedEncodingException e) {
            revObj.handleExceptionMsg(e);
            e.printStackTrace();
        }
catch (HttpException e) {
            revObj.handleExceptionMsg(e);
            e.printStackTrace();
        }
 catch (IOException e) {
            revObj.handleExceptionMsg(e);
            e.printStackTrace();
        }
 catch (Exception e){
            revObj.handleExceptionMsg(e);
            e.printStackTrace();
        }
finally {
            
//close the connection.
            post.releaseConnection();
        }

        
        
        
return revObj;
    }







---------------------
月下孤城
mail:eagle_daiqiang@sina.com

评论

# re: httpclient 实现java后台post xml请求  回复  更多评论   

2014-05-05 18:25 by zuidaima
请参考代码:java apache commons HttpClient发送get和post请求的学习整理,下载地址:http://www.zuidaima.com/share/1754065983409152.htm

只有注册用户登录后才能发表评论。


网站导航: