如何使用php和java从代理服务器连接?

时间:2012-07-03 08:50:08

标签: java php proxy

我必须使用php和java .....通过代理服务器连接到互联网。

因为我们必须在我的项目中使用各种文件中的互联网连接。

有一种方法可以在公共文件中编写通过代理服务器连接的程序,我们可以在任何地方调用该文件吗?

对于php和java ..

任何人都可以帮助我吗?

提前致谢...

1 个答案:

答案 0 :(得分:0)

Google是你的朋友!

PHP http://www.php.net/manual/en/function.stream-context-create.php#92586

$opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.php.net', false, $context);
echo $data;

JAVA http://jaydeepm.wordpress.com/2009/08/08/url-connection-via-proxy-the-java-1-5-way/

String proxyHost = "proxy.xyz.co.in"; //replace with your proxy server name or IP
int proxyPort = 8080; //your proxy server port
SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
Proxy httpProxy = new Proxy(Proxy.Type.HTTP, addr);

URLConnection urlConn = null;
BufferedReader reader = null;
String response = "";
String output = "";
URL url = new URL("www.google.com");
//Pass the Proxy instance defined above, to the openConnection() method
urlConn = url.openConnection(httpProxy); 
urlConn.connect();
reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
response = reader.readLine();
while (response!=null) {
    output+= response;
    response = reader.readLine();
}   
System.out.println("Output: " + output);