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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了