duandan1995 2012-07-03 08:50
浏览 37
已采纳

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

I have to connect to internet through proxy server using php and java.....

As we have to use internet connection in my project in various files.

There is any way to write program for connect through proxy server in a common file and we can call that file wherever we wants?

For both php and java..

Can any one help me?

Thanks in advance...

  • 写回答

1条回答 默认 最新

  • doushang8846 2012-07-03 10:10
    关注

    Google is your friend!

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler