0
ソケットを使用してHTTPClient接続を作成しようとしていますが、それを把握することはできません。コードを実行すると、次のメッセージが表示されます。JavaでのHTTPクライアント接続
HTTP/1.1 400 Bad Request
Date: Sun, 22 Apr 2012 13:17:12 GMT
Server: Apache/2.2.16 (Debian)
Vary: Accept-Encoding
Content-Length: 317
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>
<hr>
<address>Apache/2.2.16 (Debian) Server at pvs.ifi.uni-heidelberg.de Port 80</address>
</body></html>
コードはここにある:
import java.net.*;
import java.io.*;
public class a1 {
public static void main (String[] args) throws IOException {
Socket s = null;
try {
String host = "host1";
String file = "file1";
int port = 80;
ここで私はソケットを作成しています:
s = new Socket(host, port);
OutputStream out = s.getOutputStream();
PrintWriter outw = new PrintWriter(out, false);
outw.print("GET " + file + " HTTP/1.1\r\n");
outw.print("Accept: text/plain, text/html, text/*\r\n");
outw.print("\r\n");
outw.flush();
InputStream in = s.getInputStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(inr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch (UnknownHostException e) {}
catch (IOException e) {}
if (s != null) {
try {
s.close();
}
catch (IOException ioEx) {}
}
}
}
を任意の助けが理解されるであろう。ありがとうございました。
これを試してください:http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java/1359700#1359700 – duffymo
また、Apache Commons httpを使用することもできます。 – bmargulies
本当にあなた自身のHTTPクライアントを作成したいのであれば(これは面白いだけでなく、上のコメントを見てください)、まずHTTP 1.0で試して、要求されたパスの前にスラッシュを追加してください: ' "GET /" + file + "HTTP/1.0 \ r \ n" '。 –