使用Http客户端

时间:2013-04-20 06:26:32

标签: java web-services http httpclient

我是网络服务的新手,但有些人在阅读了一些文档后成功创建了一个Web服务。 我可以通过以下方式获取wsdl文件: 的 //?WSDL。 生成的WSDL还包含我的方法(API),我可以用SOAP UI测试它。 但现在我需要在浏览器中获得响应,所以我决定使用HTTP客户端作为休闲: -

HttpClient httpClient = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(
    "http://<localhost>/<serviceName>/getCustomerAttributesById?CustomerId=60000");
    HttpResponse response = httpClient.execute(getRequest);


    BufferedReader rd = new BufferedReader
      (new InputStreamReader(response.getEntity().getContent()));

    String line = "";
    while ((line = rd.readLine()) != null) {
      System.out.println("o/p Line:"+line);
    } 

但是o / p线总是空的。可能是什么原因。请帮助我。

2 个答案:

答案 0 :(得分:0)

您可以尝试在http连接上设置超时。这是示例代码。

HttpGet getRequest = new HttpGet(
"http://<localhost>/<serviceName>/getCustomerAttributesById?CustomerId=60000");

HttpParams httpParameters = new BasicHttpParams();
int timeout = 50000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
HttpConnectionParams.setSoTimeout(httpParameters, timeout);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(getRequest);

BufferedReader rd = new BufferedReader
  (new InputStreamReader(response.getEntity().getContent()));

String line = "";
while ((line = rd.readLine()) != null) {
  System.out.println("o/p Line:"+line);
} 

答案 1 :(得分:0)

如果网址正确且服务实际返回某些内容,它看起来应该有效。您可能想要检查响应状态,看看它是否实际返回200(OK)。或者将URL粘贴到浏览器中,然后查看返回的内容。

如果所有其他方法都失败了,您可能需要打开httpclient的调试日志记录。 http://hc.apache.org/httpcomponents-client-ga/logging.html

顺便说一句。您可能需要考虑使用ResponseHandler,您的代码可能无法正确释放连接。