在Java中转发HTTP请求

时间:2014-03-01 05:49:17

标签: java sockets http proxy

所以基本上我正在编写一个简单的代理,它接受方法名称为小写的HTTP请求,我必须将它们大写并将它们转发到服务器。但是转发到服务器部分是我遇到的麻烦。在我的代码中,这是“forwardToServer”方法。我已设法规范化请求,获取主机的IP地址,但是当我尝试将请求发送到主机时,我得到连接超时异常。我对套接字编程很新,所以我真的不知道发生了什么。

package proxy;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class WebProxy {

public String requestNormalizer(String badRequest) {

    return badRequest.replace(badRequest.substring(0, badRequest.indexOf(" ")), badRequest
            .substring(0, badRequest.indexOf(" ")).toUpperCase());
}

public void requestFromClient(String badRequest) throws IOException {

    ServerSocket welcome = new ServerSocket(9001);

    while (true) {

        Socket connection = welcome.accept();

        BufferedReader clientInput = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

    }
}

public void fowrardToServer(String request) throws UnknownHostException, IOException {

    Socket client;

    if (!dnsQuery(request)[1].equals(""))
        client = new Socket((InetAddress) dnsQuery(request)[0], (int) dnsQuery(request)[1]);
    else
        client = new Socket((InetAddress) dnsQuery(request)[0], 9001);

    DataOutputStream output = new DataOutputStream(client.getOutputStream());

    BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));

    output.writeBytes(request);

    System.out.println(input.readLine());
}

public Object[] dnsQuery(String request) throws UnknownHostException {

    Object[] addressPort = new Object[2];
    String hostname = request.substring(request.indexOf("host") + 6);
    hostname = hostname.substring(0, hostname.indexOf("\r"));

    if (hostname.contains(":")) {

        hostname = hostname.substring(0, hostname.indexOf(":"));
        addressPort[1] = hostname.substring(hostname.indexOf(":"));
    }
    else
        addressPort[1] = "";

    addressPort[0] = InetAddress.getByName(hostname);

    return addressPort;
}

这是异常的堆栈跟踪:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at proxy.WebProxy.main(WebProxy.java:99)

这是我用于测试目的的GET请求:

GET http://www.uga.edu/ HTTP/1.1
Host: www.uga.edu
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:18.0) Gecko/20100101
Firefox/18.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate

提前致谢!

编辑: 我用来测试它的代码非常常规,我只是测试forwardToServer方法

public static void main(String[] args) throws IOException {

    WebProxy wp = new WebProxy();
    String request = "get http://www.uga.edu/ HTTP/1.1\r\nhost: www.google.com\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate";

    wp.forwardToServer(wp.requestNormalizer(request));
}

1 个答案:

答案 0 :(得分:0)

来自套接字级别(而不是HTTP级别)的“连接超时”错误这一事实意味着网络级别存在一些阻止TCP连接建立的问题。

  • 这并不是说您正在使用的IP /端口上没有监听连接...因为这会导致“连接被拒绝”错误。

  • (可能)并非您拥有伪造的主机名或IP地址,因为这会产生不同的消息。

  • 最可能的解释是连接被客户端,服务器或其间的防火墙阻止。

尝试使用Web浏览器连接到同一主机/端口...或某些允许您打开简单TCP / IP连接的命令行工具。 (telnet命令通常在Linux上用于此...)


请注意,stacktrace表示您正在WebProxy类的main方法中创建一个Socket对象...但是您没有向我们展示该代码。实际上,根据堆栈跟踪,您包含的代码的 none 与您当前的问题直接相关。