Fabii
Fabii

Reputation: 3890

Server returned HTTP response code: 403 for URL:(How do I fix this?)

I keep keep getting the above error when I run the code below. All signs point to a problem with COOKIES from what I've read. If I am correct,how would I go about Implementing the CookieManager to fix this issue? Or how would I fix the issue if it is not an issue with COOKIES?

public class Client {

    public Client(){
    }

    String executePost(String targetURL, String urlParameters){
        URL url;
        HttpURLConnection connection = null;

        try{
            //Create connection
            url = new URL(targetURL);
            connection = (HttpURLConnection)url.openConnection();
            connection.setChunkedStreamingMode(0);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", 
                    "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", "" + 
                    Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US"); 

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            //send Request 
            DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
            dataout.writeBytes(urlParameters);
            dataout.flush();
            dataout.close();

            //get response
            InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();

            while((line = br.readLine()) != null){
                response.append(line);
                response.append('\n');

            }
            System.out.println(response.toString());
            br.close();
            return response.toString();
        }catch(Exception e){
            System.out.println("Unable to full create connection");
            e.printStackTrace();
            return null;
        }finally {

            if(connection != null) {
                connection.disconnect(); 
            }
        }
    }
}

Upvotes: 0

Views: 27615

Answers (3)

Alok Swain
Alok Swain

Reputation: 6519

If you see the API documentation of setChunkedStreamingMode, it has been mentioned there that not all servers support this mode. Are you sure that the server you are making a connection to supports this ?

Upvotes: 1

Fabii
Fabii

Reputation: 3890

I removed : connection.setChunkedStreamingMode(0); and the code worked as it should

String executePost(String targetURL, String urlParameters){

    URL url;
    HttpURLConnection connection = null;        
    try{
        //Create connection

        url = new URL(targetURL);
        connection = (HttpURLConnection)url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
                   "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Language", "en-US"); 
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //send Request 
        DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
        dataout.writeBytes(urlParameters);
        dataout.flush();
        dataout.close();

        //get response
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();

        while((line = br.readLine()) != null){
            response.append(line);
            response.append('\n');

        }
        System.out.println(response.toString());
        br.close();
        return response.toString();
    }catch(Exception e){
        System.out.println("Unable to full create connection");
        e.printStackTrace();
        return null;
    }finally {

          if(connection != null) {
            connection.disconnect(); 
          }
    }


}

Upvotes: 3

Stephen C
Stephen C

Reputation: 718678

A 403 response means "forbidden".

All signs point to a problem with COOKIES from what I've read. If I am correct,how would I go about Implementing the CookieManager to fix this issue? Or how would I fix the issue if it is not an issue with COOKIES?

It is not as simple as that.

  • It may be that you need to supply valid credentials (in the form of cookies, basic auth headers, or something else). However, you expect the server to respond with a 401 in that case, or a 302 to redirect your browser the a login page.

  • It may also be that you've supplied credentials, and they are not sufficient for the request you are trying to perform.

Your best bet is to figure out exactly what is happening when you try to login and use the service from your web browser. Then try to replicate that. Alternatively, read the site documentation or ask the site admins what to do.

If it is your site / server that you are trying to access, then you need to figure out how security is implemented. Perhaps you've misconfigured the server, or neglected to set up / enable login.


It is unlikely (IMO) that you will solve this problem by just setting up a Cookie Manager.

Upvotes: 2

Related Questions