簡體   English   中英

如何在java中使用代理獲取URL連接?

[英]How to get URL connection using proxy in java?

我試圖在運行時使用代理創建URL連接。 我的代碼如下:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =
    (HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);

但這不起作用。 誰知道為什么?

添加答案以幫助未來的訪客

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "text/xml");
connection.setRequestProperty("Accept", "text/xml, application/xml");
connection.setRequestMethod("POST");

dku.rajkumar的方式不適用於我。

我試試這個,它的工作原理。 但它需要兩倍的時間。

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));

    HttpURLConnection connection =
        (HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);
    ((HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy)).getInputStream();

System.out.println(connection.usingProxy());

結果是真的

without ((HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy)).getInputStream();

結果是錯誤的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM