一个简单的Http客户端Demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010564366/article/details/71170478

jar包:commons-httpclient-3.1.jar

请求部分代码:

String url = createUrl(systemParam);//请求地址 例如http://127.0.0.1:8080/oppf?xxxx=xxx&xxx=xx     
String body = busiparam;//报文体
String response  = null;//出参

HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
PostMethod postMethod = new PostMethod(url);
try {
    if (StringUtils.isNotBlank(body)) {
        RequestEntity entity;
        entity = new StringRequestEntity(body, "application/" + "xml", "utf-8");
        postMethod.setRequestEntity(entity);
    }
    int statusCode = httpClient.executeMethod(postMethod);

    if (statusCode != HttpStatus.SC_OK) {
        throw new Exception("Method failed:"  + postMethod.getStatusLine());
    }
    byte[] responseBody = postMethod.getResponseBody();
    response = new String(responseBody, "utf-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
展开阅读全文

没有更多推荐了,返回首页