使用HttpClient
所需jar:commons-codec-1.9.jar,commons-httpclient-3.1.jar
try {
HttpClient client = new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true));
HttpMethod method = null;
String uri = "{接口的uRL直接带参数}}";
method = new GetMethod(uri);
//浏览器的requestHeader 模拟浏览器
method.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36");
method.setRequestHeader("Accept","image/webp,image/*,*/*;q=0.8");
method.setRequestHeader("Cookie","ORA_BIPS_LBINFO=157b18e65d9; ORA_BIPS_NQID=jifecsdccfgkctabqnhk687a48hdaqsthos17jofllohscns");
try {
//执行getMethod
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "+ method.getStatusLine());
}
//读取内容
String dataXml = method.getResponseBodyAsString();
//处理内容
System.out.println(new String(dataXml));
JSONObject json = Xml2JsonUtil.xml2JSON(dataXml);
response.setContentType("text/plain; charset=UTF-8");
response.getWriter().write(json.toString());
response.getWriter().flush();
response.getWriter().close();
} catch (HttpException e)
{
/ /发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
e.printStackTrace();
} finally {
//释放连接
method.releaseConnection();
}
}catch(Exception e)
{
e.printStackTrace();
}