2012-10-15 282 views
0

我在通过代理在浏览器上显示网站时遇到问题。我从Internet选项手动将我的代理设置为127.0.0.1:80。在代码中,当我连接到网站时,我可以获取html代码并将其打印在我的java控制台上。但是,当我将html代码发送到我的浏览器时,我可以看到它连接到网站并显示“Welcome to Facebook”这样的标题。但我看不到内容。有时我只看到文字而不是图像或其他东西。显示网页内容存在问题。我无法弄清楚。也许你可以帮助我。另外我认为我无法获得UTF-8格式的内容。谢谢。通过代理服务器通过java连接到网站

 try { 
         URL url = new URL("" + req.url); 
         URLConnection urlConnection = url.openConnection(); 
         DataInputStream dis = new DataInputStream(urlConnection.getInputStream()); 
         String inputLine; 

         while ((inputLine = dis.readLine()) != null) { 
         // System.out.println(inputLine); 
          out.writeUTF(inputLine); 

         } 
         dis.close(); 
        } catch (MalformedURLException me) { 
         System.out.println("MalformedURLException: " + me); 
        } catch (IOException ioe) { 
         System.out.println("IOException: " + ioe); 
        } 

这是我如何发送线路浏览器。

private DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream()); 

回答

1

您可以在URL连接之前在System.setProperty()之前设置java代理。

用于HTTP连接 -

System.setProperty("http.proxyHost", " 127.0.0.1"); 
System.setPropery("http.proxyPort", "80"); 

对于HTTPS连接 -

System.setProperty("https.proxyHost", " 127.0.0.1"); 
System.setPropery("https.proxyPort", "80"); 
+0

我仍然无法连接。 –

+0

任何错误或异常? –

+0

它没有给出任何错误,但当我做到这一点时也没有任何改变。我通过代理向writeUTF(inputLine)发送我从网站上读取的行。我打开一个套接字将这些行发送到我的浏览器。所以我不明白为什么我仍然不能显示这些网页 –