0
我是計算機網絡新手,我有關於創建java http客戶端示例的練習。Java http請求客戶端示例
的運動指導:
Socket soc = new Socket(host, port);
DataInputStream in = new DataInputStream(soc.getInputStream());
BufferedWriter out= new BufferedWriter(new
OutputStreamWriter(soc.getOutputStream()));
out.write(httpRequest);
out.flush();
String httpResponse= in.readUTF();
,這是我的代碼:
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyTest{
public static void main(String[] args) throws UnknownHostException, IOException{
String host = "gg.gg";
String httpRequest = "GET/HTTP/1.1 Host:gg.gg ";
int port = 80;
Socket soc = new Socket(host, port);
DataInputStream in = new DataInputStream(soc.getInputStream());
BufferedWriter out= new BufferedWriter(new
OutputStreamWriter(soc.getOutputStream()));
out.write(httpRequest);
out.flush();
String httpResponse = in.readUTF();
System.out.println(httpResponse);
//soc.close();
}
}
但是當我運行程序,它運行很長一段時間,我發現,是的readUTF()方法。 它運行20s左右再顯示此消息:
請問我的請求「GET/HTTP/1.1主機:gg.gg」不正確,或任何其他的錯誤嗎? 我想使用說明表單,而不是另一種解決方案。謝謝! (我的英語不太好)