I do both the browser and third-party sites request:

(from https://www.hurl.it/ )

GET https://********* HEADERS Accept: */* Accept-Encoding: gzip, deflate User-Agent: runscope/0.1 

I get the answer: 302 Moved Temporarily . All right.

Now I make the same request for Java (for the purity of the experiment I copied all the headers):

  HttpURLConnection connection = (HttpURLConnection) new URL("https://*****").openConnection(); connection.setRequestProperty("Accept"," */*"); connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); connection.setRequestProperty("User-Agent", "runscope/0.1"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder page = new StringBuilder(); String read; while ((read = in.readLine()) != null) page.append(read); System.out.println(connection.getResponseCode()); 

I get the answer 200 . Checked on other requests. Plus, on this request, the site should send set-cookies, it does not send them to Java. What's wrong?

UPD. Just make a request manually through a socket. Received 302 , as it should be. What is wrong with this HttpURLConnection ? Can someone tell me how to see what he actually sends to the server?

    1 answer 1

    The default HttpURLConnection set to

     /* do we automatically follow redirects? The default is true. */ private static boolean followRedirects = true; 

    Those. All requests returned 3xx will be re-requested at the new address.

     HttpURLConnection connection = (HttpURLConnection) new URL("https://*****").openConnection(); connection.setRequestProperty("Accept"," */*"); connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); connection.setRequestProperty("User-Agent", "runscope/0.1"); int status = connection.getResponseCode(); // тут можно проанализировать код // дальше идет код который подразумевает, что было перенаправление // новый URL String newUrl = connection.getHeaderField("Location"); // полученные куки String cookies = connection.getHeaderField("Set-Cookie"); connection = (HttpURLConnection) new URL(newUrl).openConnection(); connection.setRequestProperty("Cookie", cookies); connection.setRequestProperty("Accept"," */*"); connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); connection.setRequestProperty("User-Agent", "runscope/0.1"); // дальше можно делать как делали вы 

    If you want more control over the situation, then you should use the HttpClient, for example Apache Http Client Library

    • As to me it did not immediately reach) I cut down this redirect through connection.setInstanceFollowRedirects (false); and everything is fine, thank you) - Uraty
    • @Uraty I slightly expanded the example - Mikhail Vaysman