![](/img/trans.png)
[英]java httpserver handling POST requests and read the html form information
[英]Java HttpServer read timeout with subsequent requests from same app too close each other
由於Java HttpServer的問題,我要瘋了。 我使用上下文等創建服務器,然后從客戶端應用程序使用URLConnection和InputStream連接到服務器。
問題在於,當客戶端執行更多彼此之間過於接近的后續請求時,客戶端通常會(沒有明顯的邏輯)讀取超時。 為了避免這種情況,我必須從連接和下一個連接中放入sleep(5000) (為什么要設置5000?因為在5000以下仍會給出超時)。
我已經嘗試過使用Executors(與Executors.newCachedThreadPool()和Executors.newFixedThreadPool(10)一起使用 ,但什么也沒有。
使用2個客戶端的另一件奇怪的事情:
所以..問題似乎與特定的客戶端實例有關
這里有一些代碼
服務器
try {
cryptex = new ServerCryptex();
final Executor multi = Executors.newCachedThreadPool();
server = HttpServer.create(new InetSocketAddress(8888), 50);
addContexts();
server.setExecutor(multi);
server.start();
} catch (IOException ex) {
Logger.getLogger(MyHttpServer.class.getName()).log(Level.SEVERE, null, ex);
}
上下文添加了
server.createContext("/empty", new HttpHandler_Empty(this));
等等
客戶
這是執行請求的功能
public byte[] performRequest(HashMap<String, String> params, String host, int port, String handler, boolean isConnectionEncrypted) throws IOException, InterruptedException {
URL url = composeUrl(params, host, port, handler, isConnectionEncrypted);
URLConnection con = url.openConnection();
con.setConnectTimeout(300);
con.setReadTimeout(7000);
InputStream is = null;
**NO PROBLEM UNTIL HERE**
for (int i = 0; i < 5; i++) {
try {
System.out.println("open the stream, try #" + i);
**TIMEOUT ON THE NEXT LINE**
is = con.getInputStream();
break;
} catch (java.net.SocketTimeoutException ex) {
**I THOUGHT TO USE THIS WAY TO TRY 5 TIMES THE CONNECTION BEFORE GIVING ERROR TO THE USER, BUT NOTHING. **
sleep(500);
if (i == 4) {
throw ex;
}
}
}
String reply = readReply(is);
System.out.println("read reply:" + reply);
if (isConnectionEncrypted) {
return cryptex.decrypt(reply);
} else {
return reply.getBytes();
}
}
真實代碼(無5次嘗試代碼)
public byte[] performRequest(HashMap<String, String> params, String host, int port, String handler, boolean isConnectionEncrypted) throws IOException, InterruptedException {
URL url = composeUrl(params, host, port, handler, isConnectionEncrypted);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(300);
con.setReadTimeout(2000);
System.out.println("open the stream");
InputStream is = con.getInputStream();
String reply = readReply(is);
System.out.println("risposta letta:" + reply);
if (isConnectionEncrypted) {
return cryptex.decrypt(reply);
} else {
return reply.getBytes();
}
}
和錯誤
giu 12, 2018 10:58:01 PM gui.Client_GUI aggiornaUdg
GRAVE: null
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at net.ClientConnector.performRequest(ClientConnector.java:96)
at net.ClientConnector.checkKey(ClientConnector.java:43)
at net.Client.getUDG(Client.java:56)
at gui.Client_GUI.aggiornaUdg(Client_GUI.java:2407)
at gui.Client_GUI.jButton1ActionPerformed(Client_GUI.java:1917)
at gui.Client_GUI.access$000(Client_GUI.java:53)
at gui.Client_GUI$1.actionPerformed(Client_GUI.java:390)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
會是什么呢? 您有什么建議可以避免這個問題? 提前非常感謝你
我不知道問題的原因。。但是我使用Apache Java HTTP Client Library解決了
這里的代碼
public byte[] performRequest(HashMap<String, String> params, String host, int port, String handler, boolean isConnectionEncrypted) throws IOException, InterruptedException {
URL url = composeUrl(params, host, port, handler, isConnectionEncrypted);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url.toString());
// add request header
HttpResponse response = client.execute(request);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String t = "";
String s = rd.readLine();
while (s != null) {
t = t + s;
s = rd.readLine();
}
System.out.println("risposta letta:" + t);
if (isConnectionEncrypted) {
return cryptex.decrypt(t);
} else {
return t.getBytes();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.