1

私はこれを使ってJavaでhttp://www.meuhumor.com.br/を読み込もうとしています:

URL url;
        HttpURLConnection connection = null;        
        try{
            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);

            DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
            dataout.flush();
            dataout.close();

            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');
            }
            br.close();
            String html = response.toString();

どのブラウザを使用してもWebサイトにアクセスできますが、Javaでhtmlを取得しようとすると、java.io.IOExceptionが発生します。サーバーがHTTP応答コードを返しました:URLの403:

誰かがhtmlを取得する方法を知っていますか?

4

1 に答える 1

1

リクエストには本文がないため、HTTP403レスポンスを受け取る可能性がありますPOST。コードはフォームを送信しようとしているようです。フォームを送信せずにページのコンテンツを単にプルダウンすることを意図している場合は、GETリクエストを試し、Content-Typeヘッダーを削除しconnection.setDoOutput(true)、を削除して、3DataOutputStream行を削除します。

于 2012-08-22T01:13:34.613 に答える