无法使用Java Socket发送HTTP响应 [英] Trouble sending HTTP response with Java Socket
问题描述
我一直在尝试编写一个简单的Web服务器的开头,但似乎无法获得发送的响应。我已经尝试了可以想象的每种类型的输出流,但似乎没有任何效果。我很茫然。以下是我正在使用的两个类,对于一些无关的代码感到抱歉:
I've been trying to write the beginnings of a simple web server, but can't seem to get the response to get sent. I've tried every type of Output stream imaginable but nothing seems to work. I'm at a loss. Here are the two classes I'm using, sorry about some of the extraneous code:
package edu.xsi.webserver;
import java.io.IOException;
import java.net.ServerSocket;
public class WebServer {
int port;
ServerSocket server;
public WebServer(int port) throws IOException{
this.port = port;
this.server = new ServerSocket(port);
Thread t = new Thread(new ServerExec());
t.start();
}
public class ServerExec implements Runnable {
public void run(){
int i = 0;
while (true) {
try {
new WebSession(server.accept(), i++);
System.out.println("Should print before session");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try {
WebServer webServer = new WebServer(8888);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是会话类处理响应。
package edu.xsi.webserver;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class WebSession implements Runnable {
Socket client;
int num;
public WebSession(Socket client, int num) {
this.num = num;
this.client = client;
Thread t = new Thread(this);
t.start();
}
public void run() {
Scanner s = null;
DataOutputStream out = null;
try {
s = new Scanner(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
//Get all input from header
while (s.hasNextLine()) {
System.out.println(s.nextLine());
}
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.writeBytes("<html><head></head><body><h1>Hello</h1></body></html>");
s.close();
out.flush();
out.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
推荐答案
在一个非常相似的问题上,我一直在疯狂地挣扎。
I've been struggling like crazy with a very similar issue.
在墙上串起来之后,我发现我的问题是如此微不足道,我一直在墙上敲打一会儿。
After literally head-banding on the wall I found my issue was so trivial I kept on head-banging on the wall for a while.
基本上我的输入while while循环我正在检查空行,但是我忘了检查空行(并随后中断) 。
Basically in my input while loop I was checking for a null line, but I forgot to check for an empty line (and subsequently break).
不保证它对你来说也一样,但这是我的一分钱:
No guarantee it'll work the same for you but here's my one cent:
in = session.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
// this seems to be the key for me!
// somehow I never get out of this loop if I don't
// check for an empty line...
if (line.isEmpty()) {
break;
}
}
out = new BufferedWriter(
new OutputStreamWriter(
new BufferedOutputStream(session.getOutputStream()), "UTF-8")
);
out.write(OUTPUT_HEADERS + OUTPUT.length() + OUTPUT_END_OF_HEADERS + OUTPUT);
out.flush();
out.close();
session.close();
我的常数是:
private static final String OUTPUT = "<html><head><title>Example</title></head><body><p>Worked!!!</p></body></html>";
private static final String OUTPUT_HEADERS = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: ";
private static final String OUTPUT_END_OF_HEADERS = "\r\n\r\n";
我的会话变量是套接字
目的。
My "session" variable is a Socket
object.
这篇关于无法使用Java Socket发送HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!