首页 > 代码库 > JAVA_Socket_Get请求

JAVA_Socket_Get请求

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.Socket;import java.net.UnknownHostException;public class TestMain{	/**	 * @param args	 */	public static void main(String[] args)	{         //http://hk.cmbchina.com/WebAffair/?pageid=Webterms 		try		{			String host = "hk.cmbchina.com";			Socket socket = new Socket(host, 80);			// 这些是必须的			StringBuffer head = new StringBuffer();			head.append("GET " +"/WebAffair/?pageid=Webterms"+ "/ HTTP/1.1" + "\r\n");			head.append("Host:" + host + "\r\n" + "\r\n");			BufferedReader in = new BufferedReader(new InputStreamReader(					socket.getInputStream()));			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(					socket.getOutputStream()));			out.append(head.toString());			out.flush();			//测试输出
System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return; }}

  

JAVA_Socket_Get请求