0

I am trying to Connect to url using URLConnection, in java with username and password. This is the following code I am using:

package com.nivi.org.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.sun.jersey.core.util.Base64;

public class GetURLContent {
    public static void main(String[] args) {

        URL url;

        try {

            url = new URL("http://sampleurl.co.uk");

            URLConnection conn = url.openConnection();


            String username = "username";
            String password = "password";
            String Token = "zzzzzzzzzzzzzzzzzzzzz";


            System.setProperty("http.proxyHost", "proxyhostname");
            System.setProperty("http.proxyPort", "8080");


            String userpass = username + ":" + password;
            String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
            conn.setRequestProperty ("Authorization", basicAuth);
            conn.setRequestProperty ("Token", Token);


            BufferedReader br = new BufferedReader(
                               new InputStreamReader(conn.getInputStream()));

            System.out.println("br............."+br.toString());

            br.close();

            System.out.println("Done");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

======================================= I am getting the following error

java.io.IOException: Server returned HTTP response code: 403 for URL:
http://sampleurl.co.uk
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
    at com.nivi.org.client.GetURLContent.main(GetURLContent.java:63)

=======================================

First of all my question is Are these following lines in the code are correct?

  conn.setRequestProperty ("Authorization", basicAuth);
  conn.setRequestProperty ("Token", Token);
3
  • 1
    may i know which protocal u r using HHTP or https , and your are passing user name only , so you should pass user name including passowrd , for aunthentication purpose we should pass both values . Commented Nov 1, 2013 at 10:52
  • You should use a java.net.Authenticator for this. Commented Aug 14, 2020 at 1:06
  • @savan Read the code. He is passing both, and HTTP or HTTPS makes no difference. Commented Aug 14, 2020 at 1:07

1 Answer 1

1

Are these following lines in the code are correct?

It depends on how the actual website you are accessing implements its user authentication.

What you appear to be doing is a combination of HTTP Basic Authentication (i.e. the Authorization header), and something involving a non-standard header called Token. This may be sufficient, if the website supports Basic Authentication.


You should probably read the website's programmer documentation of how their web APIs work. If there isn't any such documentation available to you, use your browsers web development tools to identify the mechanism that the site is using when you log in via a web browser ... and attempt to get your client to behave the same way.


One thing to note is that sending a Basic authorization response in an HTTP request is insecure. Use an HTTPS request if that is an option.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.