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















































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