Java 使用ApacheHttpClient的SOAP请求
我正试图向URL发出SOAP请求,但出现错误:Java 使用ApacheHttpClient的SOAP请求,java,web-services,soap,apache-httpclient-4.x,Java,Web Services,Soap,Apache Httpclient 4.x,我正试图向URL发出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"
<?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. ---> 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>
soap:服务器
System.Web.Services.Protocols.SoapException:服务器无法运行
处理请求。--System.InvalidOperationException:超时
期满。获取连接之前经过的超时时间
从游泳池里。这可能是因为所有池连接
正在使用,已达到最大池大小。
在WebServicex.GlobalWeather.GetCitiesByCountry(字符串CountryName)
---内部异常堆栈跟踪的结束---
如果我在浏览器上调用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());
}
公共静态字符串dosoapost()引发IOException{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=
新的HttpPost(“http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry");
addHeader(“内容类型”、“文本/xml”);
StringBuffer=新的StringBuffer();
缓冲区。追加(“”);
缓冲区。追加(“”);
缓冲区。追加(“”);
缓冲区。追加(“”);
缓冲区。追加(“巴西”);
缓冲区。追加(“”);
缓冲区。追加(“”);
缓冲区。追加(“”);
StringEntity lenty=新的StringEntity(buffer.toString());
httpPost.setEntity(lEntity);
HttpResponse lhttppresponse=httpClient.execute(httpPost);
如果(lHttpResponse.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
抛出新的RuntimeException(“HTTP问题发布方法”+
lHttpResponse.getStatusLine().getReasonPhrase());
}
返回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>
为此:
HttpPost httpPost = new HttpPost(
"http://www.webservicex.net/globalweather.asmx");
这可能是服务器上的服务出现临时问题。我在帖子中很好地运行了代码。又发送了一次请求,但仍然有效(也就是说,我得到了一个国家/城市对列表。问题不在您发布的代码块内。它必须在其他地方。我尝试了100个并发请求,它也能正常工作。@Poom:您的代码正常。我刚刚又成功调用了一个。您如何调用此代码?
HttpPost httpPost = new HttpPost(
"http://www.webservicex.net/globalweather.asmx");