首页 > 代码库 > 使用socket模拟Http请求
使用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 <!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>?????£??????? </title><style>html{overflow-y:auto}body{font:12px arial;text-align:center;background:#fff}body,p,form,ul{margin:0;padding:0}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{padding:7px 10px 3px 0;text-align:right}#m{width:680px;margin:0 auto}#nv{font-size:16px;margin:0 0 4px;text-align:left;text-indent:117px}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:90px;text-align:left}#kw{width:404px;height:22px;padding:4px 7px;padding:6px 7px 2px\9;font:16px arial;background:url(http://www.baidu.com/img/i-1.0.0.png) no-repeat -304px 0;_background-attachment:fixed;border:1px solid #cdcdcd;border-color:#9a9a9a #cdcdcd #cdcdcd #9a9a9a;vertical-align:top}.btn{width:95px;height:32px;padding:0;padding-top:2px\9;border:0;background:#ddd url(http://www.baidu.com/img/i-1.0.0.png) no-repeat;cursor:pointer}.btn_h{background-position:-100px 0}#kw,.btn_wr{margin:0 5px 0 0}.btn_wr{width:97px;height:34px;display:inline-block;background:url(http://www.baidu.com/img/i-1.0.0.png) no-repeat -202px 0;_top:1px;*position:relative}#lk{margin:33px 0}#lk span{font:14px "????"}#lm{height:60px}#lh{margin:16px 0 5px;word-spacing:3px}#mCon{height:18px;line-height:18px;position:absolute;right:7px;top:8px;top:10px\9;cursor:pointer;padding:0 18px 0 0;background:url(http://www.baidu.com/img/bg-1.0.0.gif) no-repeat right -134px;background-position:right -136px\9}#mCon span{color:#00c;cursor:default;display:block}#mCon .hw{text-decoration:underline;cursor:pointer}#mMenu{width:56px;border:1px solid #9a99ff;list-style:none;position:absolute;right:7px;top:28px;display:none;background:#fff}#mMenu a{width:100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:none}#mMenu a:hover{background:#d9e1f6}#mMenu .ln{height:1px;background:#ccf;overflow:hidden;margin:2px;font-size:1px;line-height:1px}#cp,#cp a{color:#77c}#sh{display:none;behavior:url(#default#homepage)}</style></head> <body><p id="u"><a href=http://www.mamicode.com/"/gaoji/preferences.html">???????? | ????? ?????? ????? ????? ????MP3??? ????? ????? ?
>
说明访问成功,想要扩展更加详细的使用方法,建议熟悉HTTP的协议!
使用它的好处是,当没有必要传递的参数,我们可以不需要传递,省流量;麻烦之处是,你需要深入了解Http的协议。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。