java中如何实现远程调用http来获取返回来的数据,用的是httpClient。也就是向某网站提交些数据,并接收返回的数据。
我从apche里面看到httpClient都是用来获取网页的内容而不是处理返回来的数据。我想要用程序访问一些网站,然后可以得到这个网站返回来给我的数据。
下面是我用httpClient来获取某个网页的内容,但是这不是我想要的结果。希望你们明白我要的是什么。希望你们不要给我提供我下面的代码,这个代码在什么地方都可以看到的。
public class HttpClientSimple {
public static void main(String[] args){
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
String url = "http://192.168.1.108:8080/cyberage";
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try{
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK){
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
byte[] responseBody = getMethod.getResponseBody();
System.out.println(new String(responseBody));
}catch(HttpException e){
System.out.println("Please check your provided http address!");
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
getMethod.releaseConnection();
}
}
我从apche里面看到httpClient都是用来获取网页的内容而不是处理返回来的数据。我想要用程序访问一些网站,然后可以得到这个网站返回来给我的数据。
下面是我用httpClient来获取某个网页的内容,但是这不是我想要的结果。希望你们明白我要的是什么。希望你们不要给我提供我下面的代码,这个代码在什么地方都可以看到的。
public class HttpClientSimple {
public static void main(String[] args){
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
String url = "http://192.168.1.108:8080/cyberage";
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try{
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK){
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
byte[] responseBody = getMethod.getResponseBody();
System.out.println(new String(responseBody));
}catch(HttpException e){
System.out.println("Please check your provided http address!");
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
getMethod.releaseConnection();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货