使用socket模拟Http请求

这里举一个简单的例子,使用socket来模拟一段http访问百度代码

封装的工具类如下,方法叫XHttp()

package com.spider.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NetUtil {
	
	public static final int FORMAT_IMAGE=1;
	public static final int FORMAT_VIDEO=2;
	public static final int FORMAT_TEXT=3;
	
	public static String Http(String url)
	{
		return null;
	}
	
	public static String XHttp(String url,int port)
	{
		String ret = null;
		
		Socket mSocket=null;
		OutputStream os=null;
		InputStream ins=null;
		try {
			String host=getHost(url);
			
			mSocket=new Socket(host, port);
			os=mSocket.getOutputStream();
			ins=mSocket.getInputStream();
			
			StringBuffer sb=new StringBuffer();
			sb.append("GET ").append(url).append(" HTTP/1.1").append("\r\n");
			sb.append("Host").append(": ").append(host).append("\r\n");
			sb.append("Connection").append(": ").append("Close").append("\r\n");
			sb.append("Accept").append(": ").append("*/*").append("\r\n");
			sb.append("\r\n");//end
			
			os.write(sb.toString().getBytes());
			os.flush();
			
			sb=new StringBuffer();
			BufferedReader br=new BufferedReader(new InputStreamReader(ins));  
			String str=null;  
			while((str=br.readLine())!=null)  
			{  
				sb.append(str);
				System.out.println(str);  
			};
			br.close();
			ret=sb.toString();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null!=mSocket)
			{
				try {
					mSocket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(null!=os)
			{
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(null!=ins)
			{
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return ret;
	}
	
	public static String getHost(String url)
	{
		Pattern p = Pattern.compile("(http://|https://)?([^/]*)",Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(url);
		return m.find()?m.group(2):url;
	}
}

测试代码如下:

package com.spider.test;

import com.spider.net.NetUtil;


public class TestMain {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		NetUtil.XHttp("http://www.baidu.com", 80);

	}

}

输入结果如下:

HTTP/1.1 200 OK
Date: Sat, 17 May 2014 12:55:12 GMT
Server: Apache
P3P: CP=" OTI DSP COR IVA OUR IND COM "
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Set-Cookie: BAIDUID=7EF751AE135FF261360F9E448B0F01F2:FG=1; expires=Sun, 17-May-15 12:55:12 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1
Set-Cookie: BAIDUID=7EF751AE135FF261C97AD369F7B6986E:FG=1; expires=Sun, 17-May-15 12:55:12 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1
Last-Modified: Tue, 31 Dec 2013 11:13:44 GMT
ETag: "1cdb-4eed2a7498a00"
Accept-Ranges: bytes
Content-Length: 7387
Cache-Control: max-age=1
Expires: Sat, 17 May 2014 12:55:13 GMT
Vary: Accept-Encoding,User-Agent
Connection: Close
Content-Type: text/html

�ٶ�һ�£����֪��      

�������� | ��¼



说明访问成功,想要扩展更加详细的使用方法,建议熟悉HTTP的协议!


使用它的好处是,当没有必要传递的参数,我们可以不需要传递,省流量;麻烦之处是,你需要深入了解Http的协议。



你可能感兴趣的:(Android,基础教程)