With WebClient's built-in browser, you can perform page fetching, sometimes you may need to set up an agent,
WebClient WebClient = new WebClient (browserversion.x);
Webclient.setproxyconfig (Proxyconfig pc);
In a single-threaded scenario, there is no problem with the WebClient created: the client-to-proxy connection can be set up and shut down in sequence.
Consider a situation in which multiple threads are accessing WebClient concurrently and may report the following exception:
[Thread-7] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-closing Connection [httproute[{}->http:// 192.168.5.29:3128->http://58.223.139.151:8080]][null]
........
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-total connections kept alive : 0
2012-04-19 10:31:32,926 [Thread-8] DEBUG Org.apache.http.impl.conn.tsccm.connpoolbyroute-total issued connections:0
2012-04-19 10:31:32,926 [Thread-8] DEBUG Org.apache.http.impl.conn.tsccm.connpoolbyroute-total Allocated connection: 0 out of 20
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-no free connections [ Httproute[{}->http://192.168.5.29:3128->http://58.223.139.151:8080]][null]
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-available capacity:2 out of 2 [httproute[{}->http://192.168.5.29:3128->http://58.223.139.151:8080]] [NULL]
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-creating New Connection [ HTTPROUTE[{}->HTTP://192.168.5.29:3128->HTTP://58.223.139.151:8080]]
2012-04-19 10:31:32,926 [Thread-6] DEBUG Org.apache.http.impl.client.defaulthttpclient-socket closed
Java.net.SocketException:socket closed
At java.net.SocketInputStream.socketRead0 (Native Method)
At Java.net.SocketInputStream.read (Unknown Source)
At Org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer (abstractsessioninputbuffer.java:130)
At Org.apache.http.impl.io.SocketInputBuffer.fillBuffer (socketinputbuffer.java:127)
At Org.apache.http.impl.io.AbstractSessionInputBuffer.readLine (abstractsessioninputbuffer.java:233)
At Org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine (loggingsessioninputbuffer.java:100)
At Org.apache.http.impl.conn.DefaultResponseParser.parseHead (defaultresponseparser.java:98)
At Org.apache.http.impl.io.AbstractMessageParser.parse (abstractmessageparser.java:210)
At Org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader (Abstracthttpclientconnection.java : 271)
At Org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader (defaultclientconnection.java:227)
At Org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader (abstractclientconnadapter.java:209 )
At Org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse (httprequestexecutor.java:292)
At Org.apache.http.protocol.HttpRequestExecutor.execute (httprequestexecutor.java:126)
At Org.apache.http.impl.client.DefaultRequestDirector.execute (defaultrequestdirector.java:483)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:641)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:597)
At Com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse (httpwebconnection.java:134)
At Com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection (webclient.java:1406)
At Com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection (webclient.java:1460)
At Com.gargoylesoftware.htmlunit.WebClient.loadWebResponse (webclient.java:1325)
At Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:304)
At Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:370)
Exception information display: Thread-6 When using WebClient, the socket closed exception is detected, view the above exception, there is a socket (http://192.168.5.29:3128->http:// 58.223.139.151:8080) was thread-7 closed, Thread-8 created a new socket, maybe a point in time, the socket was closed, causing thread-6 to report the socket closed exception.
You can avoid these problems by using threadlocal to create separate WebClient objects for different threads:
[Java]View Plaincopy
- Each thread maintains a separate WebClient object that prevents the thread from sharing a browser that interferes with each other
- private Threadlocal<webclient> client = new threadlocal<webclient> () {
- protected synchronized WebClient InitialValue () {
- WebClient WebClient = new WebClient (version);
- //Set parameters for WebClient
- Webclient.set ...;
- return webClient;
- }
- };
- public void Setwebclient (WebClient WC) {
- Client.set (WC);
- }
- Public WebClient getwebclient () {
- return Client.get ();
- }
WebClient a solution to the problem of socket closed in multi-threaded and agent-use situations [Htmlunit]