简体   繁体   中英

Server does not respond to concurrent requests in HTTP/1.1 Client using Java

I'am trying to implement a simple HTTP/1.1 client application against a remote HTTP server. If I have a 301 Moved Permanently response from server, I will try to download the file from it's new location given in server's response. I am able to send first GET message to server and retrieve the new URL where the file I asked was moved.

The problem is that when I send second GET request from my client with new location of the file, server returns null . Not sure if anything goes wrong with writing the client message or reading the server response. Here is my code, any help is appreciated.

else if(serverMessage.equals("HTTP/1.1 301 Moved Permanently"))
{
     System.out.println(" A new permanent URL is assigned to the file " + fileName);
     serverMessage="";
     lineCount=0;
     while((serverMessage = reader.readLine()) != null)
     {          
         lineCount++;
         System.out.println("reply: " + serverMessage);
         if(serverMessage.indexOf("Location") >= 0 )
         {
              for(int x=serverMessage.indexOf("Location")+10; x<serverMessage.length(); x++)
              {
                      newURL= newURL + serverMessage.charAt(x); 
              }
          }

      }
     System.out.println("newURL : " + newURL);
     host = findHost(newURL);

     path = findPath(newURL);

     fileName=findFileName(newURL);                   

     clientMessage = "GET ";
     clientMessage = clientMessage + path;
     clientMessage = clientMessage + " HTTP/1.1\r\nHost: ";
     clientMessage = clientMessage + host;
     clientMessage = clientMessage + "\r\n\r\n";

     System.out.println("client message: \"" + clientMessage +"\"");

     writer.newLine();
     writer.write(clientMessage);
     writer.flush();

     serverMessage = reader.readLine();
     System.out.println("reply2: " + serverMessage); //returns null!!!

     while((serverMessage=reader.readLine())!=null)
     {
           System.out.println("reply2: " + serverMessage);
     }

}

EDIT: Variables of client message are the followings (they all work correctly, tested for existing file - successfully downloaded!)

newURL : http://wlab.cs.bilkent.edu.tr/~cs421/pa1/302-redirect-success.txt
host2: wlab.cs.bilkent.edu.tr
path2: /~cs421/pa1/302-redirect-success.txt
fileName2: 302-redirect-success.txt

Are you using a persistent URLConnection / HttpURLConnection ?

You may be receiving null if the connection has been closed by the server.

If you are using persistent connections, the server might have not had the time to respond.

This might describe the problem a little better. Check out the timeout given in doHttpUrlConnectionAction(String desiredUrl) . You might find the answer there.

If this is your problem, you can try to do multiple reads at 0.1 second intervals for say ... 1-5 seconds. This is to make sure you get the response fast and don't have to wait the full timeout to make sure that the server has responded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM