java socket请求服务器出错,信息我贴出来了,求大神帮帮看看
HTTP/1.1 400 Bad Request
Date: Mon, 18 Mar 2013 16:02:10 GMT
Server: Apache/2.2.9 (Win32) PHP/5.2.6
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>
没有想到哪里出错了,求大神赐教 socket java server
[解决办法]
这是 HTTP response 的信息,
你要贴出 HTTP request 的信息,才有可能分析。
[解决办法]
应该是你协议的格式有问题
发段代码案例给你
/**
* 尝试获取一个domain下的资源,根据http1.0协议尝试读取内容 当然,如果返回的是500系列的异常,那么将不会获取内容
*
* @param domain 域名或ip
* @param resource uri统一资源标记符
* @param port 端口
* @return
*/
public static final List<String> readHttpContent(String domain, String resource, int port) {
Socket socket = null;
try {
socket = new Socket(domain, port);
PrintWriter out = new PrintWriter(socket.getOutputStream());
StringBuilder send = new StringBuilder();
send.append("GET " + resource + " HTTP/1.0").append("\n");
send.append("Accept: text/html").append("\n");
send.append("\n");
out.write(send.toString());
out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
List<String> result = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.contains("HTTP/1.1 5")) {
return Collections.emptyList();
}
result.add(line);
}
return result;
} catch (Exception ex) {
return Collections.emptyList();
} finally {
if (socket != null) {
try {
socket.close();
} catch (Exception ex) {
}
}
}
}