java - 服务器返回 HTTP 响应代码 : 403 for URL:(How do I fix this? )

标签 java http-status-code-403

当我运行下面的代码时,我不断收到上述错误。从我读到的内容来看,所有迹象都表明 COOKIES 存在问题。如果我是正确的,我将如何实现 CookieManager 来解决这个问题?或者,如果不是 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(); 
            }
        }
    }
}

最佳答案

我删除了:connection.setChunkedStreamingMode(0);并且代码按其应有的方式工作

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(); 
          }
    }


}

关于java - 服务器返回 HTTP 响应代码 : 403 for URL:(How do I fix this? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9553823/

相关文章:

Java 游标仅给出 50 行

java - 澄清 - 在 Eclipse 中导入语句

java - 自定义 TextView 不显示文本但仅显示背景

java - 当一个项目的Classpath中缺少一些使用的类时,项目怎么可能运行良好呢?

java - hibernate继承映射single_table与 boolean 类型的属性

php - (403) 权限不足,无法在 gmail api 上发送电子邮件

wordpress - 403 禁止 : permission to access images

javascript - 使用 JS 检测特定错误(403)

java - URL 的 commons io 403 但 httpclient 没问题

apache - 缺少目录索引时的 404 与 403