为网址openStream()返回403的服务器 [英] Server returning 403 for url openStream()

查看:164
本文介绍了为网址openStream()返回403的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.net.URL; 
import java.io. *;
导入java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public static void main(String args []){
try {
processHTMLFromLink(new URL(http://fwallpapers.com));
} catch(MalformedURLException ex){
Logger.getLogger(Test.class.getName()).log(Level.SEVERE,null,ex);
}
}

public static int processHTMLFromLink(URL url){
InputStream is = null;
DataInputStream dis;
字符串行;
int count = 0;
尝试{
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); ((line = in.readLine())!= null){
System.out.println(line);

while
}
} catch(MalformedURLException mue){
System.out.println(mue.toString());
} catch(IOException ioe){
System.out.println(ioe.toString());
} finally {
try {
is.close();
} catch(IOException ioe){
//在这里没有看到
}
}
return count;


$ / code $ / pre
$ b $ p

  java.io.IOException:服务器返回的HTTP响应代码:403 for URL:http://fwallpapers.com 
线程mainjava中的异常。 lang.NullPointerException在Test.processHTMLFromLink(Test.java:38)处
在Test.main(Test.java:15)处

Java结果:1

它在浏览器上正常工作。但我得到空点异常。此代码可以与其他链接一起正常工作任何人都可以帮我解决这个问题。如何获得内容,而我得到403错误。

解决方案

这是一个旧的帖子,但如果人们想知道这是如何作品。



<403>意味着遭遇拒绝。
有一个解决这个问题的方法。
如果你想做到这一点,你必须设置一个用户agant参数来'欺骗'网站



这就是我的旧方法的样子:

  private InputStream read(){
try {
return url.openStream();
}
catch(IOException e){
String error = e.toString();
抛出新的RuntimeException(e);


$ / code $ / pre

将其更改为:(它适用于我! )

  private InputStream read(){
try {
HttpURLConnection httpcon =(HttpURLConnection)url.openConnection ();
httpcon.addRequestProperty(User-Agent,Mozilla / 4.0);

返回httpcon.getInputStream();
} catch(IOException e){
String error = e.toString();
抛出新的RuntimeException(e);
}
}


import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
    public static void main(String args[]) {
        try {
            processHTMLFromLink(new URL("http://fwallpapers.com"));
        } catch (MalformedURLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static int processHTMLFromLink(URL url) {
        InputStream is = null;
        DataInputStream dis;
        String line;
        int count = 0;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException mue) {
            System.out.println(mue.toString());
        } catch (IOException ioe) {
            System.out.println(ioe.toString());
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }
        return count;
    }
}

error:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://fwallpapers.com
Exception in thread "main" java.lang.NullPointerException
    at Test.processHTMLFromLink(Test.java:38)
    at Test.main(Test.java:15)
Java Result: 1

It is working fine on browser. But I am getting null point exceptions. this code works fine with other links. can anyone help me out with this. How can I get content while i am getting 403 error.

解决方案

This is an old post but if people wanted to know how this works.

a 403 means acces-denied. There is a work around for this. If you want to able to do this you have to set a user agant parameter to 'fool' the website

This is how my old method looked like:

private InputStream read() {
try {
    return url.openStream();
 } 
catch (IOException e) {
  String error = e.toString();
  throw new RuntimeException(e);
 }
}

Changed it to: (And it works for me!)

private InputStream read() {
try {
    HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
    httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");

  return httpcon.getInputStream();
 } catch (IOException e) {
    String error = e.toString();
  throw new RuntimeException(e);
 }
}

这篇关于为网址openStream()返回403的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆