一个简单的Http客户端Demo

xiaoxiao2021-02-27  401

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(); }
转载请注明原文地址: https://www.6miu.com/read-3675.html

最新回复(0)