我正在尝试获取客户端访问我的服务器的地址。
如果客户端访问服务器,
http://localhost:8890
那么服务器应该准确读取http://localhost:8890
。如果客户端访问服务器,
http://127.0.0.1:8890
那么服务器应该准确读取http://127.0.0.1:8890
。如果客户端访问服务器,
http://mywebsite.com:8890
那么服务器应该准确读取http://mywebsite.com:8890
。
我通过以下功能部分获得了它:[SOCKET].getInetAddress().getHostName()
但是客户端访问http://127.0.0.1:8890
函数[SOCKET].getInetAddress().getHostName()
返回它localhost
而不是127.0.0.1
.
我需要服务器获取地址作为发送数据的客户端,因为COOKIES
不会丢失任何重定向发生的机会(302 Found
或301 Moved Permanently
)。
我到处搜索,尤其是在操作系统中,但我不知道要使用什么搜索词(如果有人已经问过这个问题),如果这是重复的,请原谅。
我的代码:
package com.[PACKAGE];
/*web server*/
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Locale;
/*Err*/
//import java.io.IOException;
public class miniWebServer {
private int port;
private ServerSocket mySocket;
private Socket remote;
private Thread trd;
public miniWebServer(int _port) {
port = _port;
initServer();
}
private void initServer(){
trd = new Thread(new Runnable(){
@Override
public void run(){
try {
//Criar socket
mySocket = new ServerSocket(port);
System.out.println("Conected!");
OK();
} catch (Exception e) {
String st = e.toString().toLowerCase(Locale.US);
if(st.indexOf("address already in use")!=-1){
System.out.println("Server ");
OK();
} else {
System.err.println("Erro ao tentar conectar com a port " + port + ": "+ e);
}
return;
}
listen();
}
});
trd.start();
}
public void OK(){}//Override
private void listen(){
String addressClient = "";
String portClient = "";
PrintWriter outServer;
for (;;) {
try {
//Esperando uma conexão (um cliente)
remote = mySocket.accept();
System.out.println("remote: "+remote.toString());
System.out.println("getLocalAddress: "+remote.getLocalAddress().toString());
System.out.println("getHostName: "+remote.getInetAddress().getHostName().toString());
System.out.println("getLocalSocketAddress: "+remote.getLocalSocketAddress().toString());
addressClient = remote.getInetAddress().getHostName().toString();
portClient = Integer.toString(remote.getLocalPort());
System.out.println("http://"+addressClient+":"+portClient);
outServer = new PrintWriter(remote.getOutputStream());
outServer.println("HTTP/1.0 200 OK");
outServer.println("Content-type: text/html");
outServer.println("");
outServer.println("<html>");
outServer.println("<body>");
outServer.println("<p>You address: http://"+addressClient+":"+portClient+"</p>");
outServer.println("</body>");
outServer.print("</html>");//last line
outServer.flush();
remote.close();
} catch (Exception e) {
//System.out.println("loop::Error: " + e);
}
try {
//Thread.currentThread();
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
如何使用:
new miniWebServer(8890){
@Override
public void OK(){
System.out.println("OK!");
}
};
解决方案:
假设我使用浏览器 GoogleChrome,我的 java 服务器将返回:
GET /folder/page.html HTTP/1.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US;q=0.6,en;q=0.4
Cache-Control: max-age=0
Connection: keep-alive
Host: localhost:8890
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
要获取标头和请求:
inServer = new BufferedReader(new InputStreamReader(remote.getInputStream()));
然后只需提取Host:
与.slit(":")
或的地址new StringTokenizer(currentLine)