Java を使用して HTTP リクエストを作成しようとしていますが、無効なメソッドを取得しており、その理由がわかりません。これが私のコードです:
String str = "GET / HTTP/1.1\r\nHost: " + this.url + "\r\n";
int i=r.nextInt(agents.length);
String uAgent = agents[i]; //agents is an array of user agents.
str = str + "User-Agent: "+uAgent+"\r\n";
str = str + "Content-Length: " + (int)(Math.random() * 1000.0D) + "\r\n"; //random content length for now
str = str + "X-a: " + (int)(Math.random() * 1000.0D) + "\r\n"; //random
HttpURLConnection con = (HttpURLConnection) new URL(this.url).openConnection();
con.setRequestMethod(str);
con.setConnectTimeout(5000); //set timeout to 5 seconds
con.connect();
System.out.print(".");
私が得ているエラーはこれです:
java.net.ProtocolException: Invalid HTTP method: GET / HTTP/1.1
Host: http://example.com/
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)
Content-Length: 434
X-a: 660
at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:339)
at jbot.HTTP.run(HTTP.java:88)
有効な方法を使用しているように見えるので、わかりません。