我正在尝试使用 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)
看来我正在使用有效的方法,所以我不知道。