使用 Apache HttpClient 的 SOAP 请求

SOAP request using Apache HttpClient

我正在尝试向 URL http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry 发出 SOAP 请求,但出现错误:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
 <soap:Fault>
   <faultcode>soap:Server</faultcode>
   <faultstring>System.Web.Services.Protocols.SoapException: Server was unable 
      to process request. ---&gt; System.InvalidOperationException: Timeout 
      expired.  The timeout period elapsed prior to obtaining a connection 
      from the pool.  This may have occurred because all pooled connections 
      were in use and max pool size was reached.
      at WebServicex.GlobalWeather.GetCitiesByCountry(String CountryName)
       --- End of inner exception stack trace ---
    </faultstring>
    <detail />
  </soap:Fault>
 </soap:Body>
</soap:Envelope>

如果我在浏览器上调用 URI,它就可以工作。我认为它在我的代码中遗漏了一些东西。

这是我正在做的事情:

public static String doSOAPPost() throws IOException{
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = 
    new HttpPost("http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry");
    httpPost.addHeader("content-type", "text/xml");

    StringBuffer buffer = new StringBuffer();
    buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    buffer.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    buffer.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
    buffer.append("<soap:Body>");
    buffer.append("<GetCitiesByCountry xmlns=\"http://www.webserviceX.NET\">");
    buffer.append("<CountryName>Brazil</CountryName>");
    buffer.append("</GetCitiesByCountry>");
    buffer.append("</soap:Body>");
    buffer.append("</soap:Envelope>");

    StringEntity lEntity = new StringEntity(buffer.toString());
    httpPost.setEntity(lEntity);

    HttpResponse lHttpResponse = httpClient.execute(httpPost);

    if (lHttpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
        throw new RuntimeException("HTTP problems posting method " + 
        lHttpResponse.getStatusLine().getReasonPhrase());
    }
    return EntityUtils.toString(lHttpResponse.getEntity());

}

我该如何解决这个错误?怎么了?

提前致谢。

我刚刚执行了你的代码,没有修改,我得到了这个回复:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
            <GetCitiesByCountryResult>
                <NewDataSet>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Conceicao Do Araguaia</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Afonsos Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Anapolis Braz-Afb</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Aracaju Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Alta Floresta Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Benjamin Constant</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Belem Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Bage Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Belo Horizonte Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Curitiba</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Barbacena</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Brasilia Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Bauru</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Boa Vista Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Barra Do Garcas</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Belo Horizonte</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Campo Grande Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Maranhao / Carolina Airport</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Porto Alegre</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Campos</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Corumba</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Curitiba Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Caravelas Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Cuiaba Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Cruzeiro Do Sul</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Presidente Prudente</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Eduardo Gomes International</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Jacareacanga</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>S. P. Aldeia Aerodrome</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Foz Do Iguacu Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Florianopolis Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Fernando De Noronha</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Fortaleza Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Gama</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Galeao</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Goiania Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Guarulhos Civ / Mil</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Guaratingueta</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Altamira</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Itaituba</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Ilheus Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Imperatriz</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Juiz De Fora</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Joao Pessoa</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Rio / Jacarepagua</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Campina Grande</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Campinas Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Londrina Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Bom Jesus Da Lapa</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Maraba</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Macae</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Maringa</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Montes Claros</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Manaus Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Maceio Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Macapa</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Mocoro / 17 Rosado</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Marte Civ / Mil</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Manicore</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Natal Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Oiapoque</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Porto Alegre Aero-Porto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Parnaiba Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Pocos De Caldas</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Passo Fundo</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Paranagua</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Pelotas</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Petrolina Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Porto Nacional Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Ponta Pora Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Porto Velho Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Vitoria Da Conquista</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Rio Branco</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Recife Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Rio De Janeiro Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Resende</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Sao Carlos</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Santa Cruz Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Sao Jose Dos Campo</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Sao Luiz Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Santa Maria Aero-Porto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Santarem-Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Sao Paulo Aeropor-To</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Santos Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Salvador Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Teresina Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Tefe</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Tarauaca</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Tabatinga</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Tucurui</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Sao Gabriel Da Cachoeira</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Paulo Afonso</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Uruguaiana Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Uberaba</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Vilhena Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Vitoria Aeroporto</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Xavantina</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Iauarete</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Pirassununga</City>
                    </Table>
                    <Table>
                        <Country>Brazil</Country>
                        <City>Barcelos</City>
                    </Table>
                </NewDataSet>
            </GetCitiesByCountryResult>
        </GetCitiesByCountryResponse>
    </soap:Body>
</soap:Envelope>

错误信息:

System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

看起来更像是后端异常(似乎服务器无法连接到 SQL 数据库)而不是与客户端相关的异常。所以 您尝试呼叫时服务器似乎出现问题

不过只有一个建议,请替换为:

HttpPost httpPost = new HttpPost(
"http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry");

有了这个:

HttpPost httpPost = new HttpPost(
"http://www.webservicex.net/globalweather.asmx");

这可能是服务器服务的暂时性问题。我运行代码在post就好了。