HttpsUrlConnection with Authorization seems to cut off url parameter in GET request

981 Views Asked by At

I'm trying to establish a Connection via HTTPS. I also set the "Authorization" property in the Request Header to Basic and provide an encoded auth string accordingly.

I checked with the Firefox Plugin HttpRequester and everythign works fine, which means I entered the url, choose "GET" as request method, add the Authorization to the header and after pressing submit I get back some xml which only a properly authorized user should get.

Unfortunately I can neither provide you with the actual auth info nor the real url in the SSCCE. However, I can tell you, that the Auth seems to work, since I get a 200 response. I also changed the Auth to a wrong value and get a "401 Authorization Required" response then.

It actually seems like the "?myparam=xyz" is somehow cut off, because when I remove this parameter from the url and test with Firefox HttpRequester again I get the same response as in Java.

Unfortunately I have no access to "theirdomain.com", so I don't know what's happending on the server side. But since it works with the Firefox HttpRequester, it should also work with Java.

What could be the reason? Thanks for your help!

EDIT: I changed the url to "https://www.google.com/search?q=foo" and commented this line:

//con.setRequestProperty("Authorization", auth);

I can see from the returned string, that google received the "foo". So apparently the combination of Authorization and get parameter seems to be the problem, since both separately work fine.

SSCCE:

    import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class HttpRequest
{

    /**
     * @param args
     */
    public static void main(final String[] args)
    {
        System.out.println("start request");
        final String urlString = "https://theirdomain.com/foo/bar/bob?myparam=xyz";
        final String auth = "Basic XyzxYzxYZxYzxyzXYzxY==";

        HttpsURLConnection con;
        try
        {

            final URL url = new URL(urlString);

            con = (HttpsURLConnection) url.openConnection();
            con.setRequestProperty("Authorization", auth);
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");

            con.setRequestMethod("GET");
            //            con.setDoOutput(true);
            con.connect();
            final int responseCode = con.getResponseCode();
            if (responseCode != 200)
                System.out.println("Server responded with code " + responseCode + " " + con.getResponseMessage());
            else
            {
                System.out.println("Starting to read...");

                final InputStream inStream = con.getInputStream();
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();

                int c;

                while (inStream != null && (c = inStream.read()) != -1)
                {
                    baos.write(c);
                }

                System.out.println(new String(baos.toByteArray()));
            }
        }
        catch (final IOException e)
        {
            System.out.println("could not open an HTTP connection to url: " + urlString);
            e.printStackTrace();
        }
        finally
        {
            System.out.println("end request");
        }
    }

}
1

There are 1 best solutions below

1
On

Have you tried adding con.setRequestProperty("myparam", "xyz"); to your code?