0

I'm new with Java and Http requests. I'm trying to get the response after I execute a URL. This is the situation: I have this URL:

https://api.instagram.com/oauth/authorize/?client_id=63092ass123&redirect_uri=http%3A%2F%2Fexample.com%2F&response_type=code&scope=basic

and after execute it in the browser, it responses with a redirect link like:

http://example.com/?code=abcd123

What I want to do is automate the process and get the response code (abc123). I've been reading about httpclient and following this [tutorial][1] but this is what I get:

HTTP/1.1 200 OK
org.apache.http.client.entity.DecompressingEntity@1e9e725a
HTTP/1.1 302 Found
[Content-Type: text/html; charset=utf-8,Content-Length: 0,Chunked: false]

I think I am not getting the right aproach, right?

EDIT: I have followed every tutorial and every response, but as I mentioned, I am new with this and I can't find the way to do that. This is what I'm actually doing and I don't know why can't get the code:

public static String clientid = "5348f1b98f60418995eb9830297dffe0";
public static String secretcli = "aa541c5013eb4ca2a93d92b46d3c0e4a";
public static String BackUri = "http://aieduca.com/";
  public static void main(String[] args) throws ClientProtocolException, IOException  {
    InstagramService service = new InstagramAuthService()
                .apiKey(clientid)
                .apiSecret(secretcli)
                .callback("http://aieduca.com/")     
                .scope("basic")
                .build();

      String authorizationUrl = service.getAuthorizationUrl();
      System.out.println("Autoriz= "+authorizationUrl);


      try {

          URLConnection con = new URL( authorizationUrl ).openConnection();
          System.out.println( "orignal url: " + con.getURL() );
          con.connect();
          System.out.println( "connected url: " + con.getURL() );
          InputStream is = con.getInputStream();
          //url2=con.toString();
          System.out.println( "redirected url: " + con.getURL() );            

          HttpURLConnection con1 = (HttpURLConnection)(new URL( authorizationUrl ).openConnection());
          con1.setInstanceFollowRedirects( false );
          con1.connect();
          int responseCode = con1.getResponseCode();
          System.out.println( "ResFinal: "+responseCode );
          String location = con1.getHeaderField( "Location" );
          System.out.println( location );

          HttpURLConnection con2 = (HttpURLConnection)(new URL( location ).openConnection());
          con2.connect();
          System.out.println( "connected url1: " + con2.getURL() );
          InputStream is1 = con2.getInputStream();
          System.out.println( "redirected url1: " + con2.getURL() );

          is.close();
        } finally {
            System.out.println("END.");

I am so frustrated so if anyone of those who had the same problem could help me, I would be very grateful.

Juliana Rivera
  • 1,013
  • 2
  • 9
  • 15
  • Possible duplicate of [Java - How to find the redirected url of a url?](http://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url) – James Jithin Mar 21 '17 at 08:37
  • You could have a look at [Scribe](https://github.com/scribejava/scribejava). Also, have a look at this [answer](http://stackoverflow.com/a/4516998/1426227). – cassiomolin Mar 21 '17 at 08:52

1 Answers1

0

You can set a RedirectStrategy when building your HttpClient and thereby disabling the automatic redirect. Have a look at this example: https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html#d5e334

Harald Gliebe
  • 7,236
  • 3
  • 33
  • 38