2017-12-20 10 views
0

を認証される必要があり、正常に動作します(JAVA)エラーは、私は、URLを開くには、このメソッドを使用してい

String osName = System.getProperty("os.name"); 
    String url = "https://abcdefg.com"; 
    try { 
     if (osName.startsWith("Windows")) { 
      Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); 
     } else if (osName.startsWith("Mac OS X")) { 
      Runtime.getRuntime().exec("open " + url); 
     } else { 
      System.out.println("SO unsupported"); 
     } 
    } catch (IOException e) { 
     System.out.println("Error opening " + url); 
     e.printStackTrace(); 
    } 

私の問題は、今私が入力しようとしているということですあなたが入力する前に認証する必要があるURL(これは私にエラー403を禁じます。あなたはこのサーバにアクセスする権限がありません)。質問は、私はURLを起動する前に認証することができるので、私はエラーが表示されません? (私はユーザー/パスを持っています) link to the image, I cant post them yet...

ありがとうございます!

PD:誰かがそれを求める前に、私は、この他の1は私に、このエラーを与えるため、URLを開くには、そのメソッドを使用します:接続リセット:P

try { 
    URL url = new URL("http://www.google.com"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
    //InputStream is = url.openConnection().getInputStream(); 
    //BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    String line = null; 
    while ((line = in.readLine()) != null) { 
     System.out.println(line); 
    } 
    in.close();  
} catch (MalformedURLException e) { 
    System.out.println("MalformedURLException-> " + e); 
} catch (IOException e) { 
    System.out.println("IOException-> " + e); 
} 

IOException-> java.net.SocketExceptionが

+0

にしてくださいどのようにですか? – johnII

答えて

0

HTTPリクエストを認証する必要がありますが、これはサーバーが要求した認証方式に依存します。

通常、URLからHttpUrlConnectionを作成し、Authenticationヘッダーを正しい値に設定する必要があります。 HTTP認証を検索します。 のようなHTTP認証方式は、実装が簡単ですが、複雑すぎるため、特定のライブラリを使用する必要があります。基本認証がサポートされていると仮定すると、

+0

これについていくつかの調査をするつもりです:) – Frikilangelo

+0

あなたがそれに満足しているなら、この質問に答えてください。 –

0

は、ここではApacheのHTTPClientまたはjsoupのようなURLでの作業のより多くのテスト方法を使用しないで、なぜ私はそれを

static String readUrl(String url, String user, String pass) throws IOException { 
    byte[] auth = (user + ':' + pass).getBytes("UTF-8"); 
    auth = Base64.getEncoder().encode(auth); 

    URLConnection conn = new URL(url).openConnection(); 
    conn.setRequestProperty("Accept-Charset", "UTF-8"); 
    conn.setRequestProperty("Accept-Encoding", "identity"); 
    conn.setRequestProperty("User-Agent", "MadeUpUserAgent/0.1"); 
    conn.setRequestProperty("Authorization", "Basic " + new String(auth)); 

    StringBuilder sb = new StringBuilder(); 
    InputStream in = conn.getInputStream(); 
    BufferedReader reader = new BufferedReader(
      new InputStreamReader(in, "UTF-8")); 

    for (String line; (line = reader.readLine()) != null;) { 
     sb.append(line).append(System.lineSeparator()); 
    } 

    return sb.toString(); 
} 
+0

私はBase64のための適切なインポートを見つけることができません、私はEclipseのgetEncoderメソッドを認識しないので、私はライブラリを探すつもりです。 私はjava 1.7.0_80-b15を持っていますが、そのより良いものは、Apacheのコモンズまたは太陽のBASE64エンコーダを使用するのですか? – Frikilangelo

+0

前のものを編集できません。まあ...最後に、_auth = Base64.getEncoder()。encode(auth); _ _String encoded = DatatypeConverter.printBase64Binaryを変更して、インポート_javax.xml.bind.DatatypeConverter_を使用します。 (auth); _と_encode_の_new String(auth)_の一部。いずれにしても、BufferedReader(_java.net.SocketException:Connection reset_)を使用すると、open URLの他のメソッドと同じ問題が発生しました。 – Frikilangelo

関連する問題