简体   繁体   English

使用Java套接字类连接到Web服务器

[英]Connecting to web server with java socket class

I am trying to create a simple web proxy app using Java(without using the HTTPUrlConnection class). 我正在尝试使用Java创建一个简单的Web代理应用程序(不使用HTTPUrlConnection类)。

I have so far managed to successfully have my server listen on port 10000, and then accept a client connection when I enter a URL into my browser. 到目前为止,我已经成功地使服务器成功侦听端口10000,然后在浏览器中输入URL时接受了客户端连接。

I now require my proxy to forward the HTTP request from the browser to the actual web server(the URL which I typed into the browser). 现在,我需要我的代理将HTTP请求从浏览器转发到实际的Web服务器(我在浏览器中键入的URL)。

However, I am getting a java.net.UnknownHostException: when attempting to create a socket connection between my proxy and the web server. 但是,在尝试在我的代理和Web服务器之间创建套接字连接时,我得到了java.net.UnknownHostException :。 Does anyone know what may be causing this issue? 有谁知道是什么原因导致此问题?

Below is the output showing the error aswell as the complete code aswell. 以下是显示错误以及完整代码的输出。 Any help is very much appreciated! 很感谢任何形式的帮助!

Starting the socket server at port:10000
Listening.....
Socket[addr=/127.0.0.1,port=64099,localport=10000]has connected
URL IS http://www.hotmail.com
Can't connect
java.net.UnknownHostException: http://www.hotmail.com

import java.net.*;
import java.io.*;

public class Proxy {

private ServerSocket serverSocket;
private int port;

public Proxy(int port) {
    this.port = port; }

 public static void main(String[] args) {
    int port = 10000;             
    try {
        // initialize the proxy
        Proxy proxy = new Proxy(port);
        proxy.start();   
        } 
    catch (IOException e) {
        e.printStackTrace();
        }
}

public void start() throws IOException {
    System.out.println("Starting the socket server at port:" + port);
    serverSocket = new ServerSocket(port);

    //Listen for client connection
    System.out.println("Listening.....");
    Socket client = serverSocket.accept();

    //A client has connected to this server
    verifyClient(client);
}

private void verifyClient(Socket client) throws IOException {   
    System.out.println(client + "has connected");
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

    //Parse the HTTP request from the browser and find the URL
    String request;
    while ((request = in.readLine()) != null) {
        if (request.contains("http://")){
            StringBuilder sb = new StringBuilder(request);
            sb.delete(0,4);
            sb.delete(sb.length()-9,sb.length());
            makeConnection(sb.toString());              
            break;}
        in.close();
    }   
}

private void makeConnection(String url) throws IOException{
//Establish connection between proxy & web server on socket
try {
    InetAddress addr;
    URL aURL = new URL(url);
    System.out.println("URL IS " + aURL.toString());
    Socket server = new Socket(urlString,80);
    addr = server.getInetAddress();
    System.out.println("IP is: " + addr);
    System.out.println("Connected to " + addr);
    server.close();
}
catch (IOException e) {
    System.out.println("Can't connect");
    System.out.println(e);
    }

} }

From your update, it seems like you are trying to create a socket connection to " http://www.hotmail.com ". 从您的更新看来,您似乎正在尝试创建到“ http://www.hotmail.com ”的套接字连接。 This should just be www.hotmail.com. 这应该只是www.hotmail.com。 The "http://" before this is a problem. 这之前的“ http://”是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM