1

httppost を使用して、xml データを返す URL に接続しています。

StringEntity myEntity = new StringEntity(inputXMLString,"text/xml","UTF-8");
HttpPost httppost = new HttpPost(requestURL);
httppost.setEntity(myEntity);
//Set Parameter
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 1200000);
HttpConnectionParams.setSoTimeout(httpParams, 1200000);
//retrieve response
HttpClient httpClient = new DefaultHttpClient(httpParams);
InetAddress addr = InetAddress.getByName(IP_ADDRESS);
httpClient.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS,addr);
long diff;
long start;
start = System.currentTimeMillis();
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inputstream = entity.getContent();
diff = (start - System.currentTimeMillis());
//Then printing diff
return inputstream;

入力ストリームは DocumentBuilder によって使用され、Documnent オブジェクトに解析されます。ほとんどの場合は問題なく動作しますが、以下のように接続例外が発生することがあります

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown         
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
   .................
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
4

0 に答える 0