HTTP原型
2017-01-26 10:14
323 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System.Drawing; namespace HTTPHelpers { class http { // 公用Cookie信息 public static CookieContainer myCookie; //返回的Html代码或结果 public static string html; /// <summary> /// Get方式提交 /// </summary> /// <param name="strUrl">目的资源链接</param> /// <returns>String:Response返回值</returns> public static string GetModel(string strUrl, string server) { string strRet = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl); request.Timeout = 2000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream resStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.UTF8; StreamReader readStream = new StreamReader(resStream, encode); Char[] read = new Char[256]; int count = readStream.Read(read, 0, 256); while (count > 0) { String str = new String(read, 0, count); strRet = strRet + str; count = readStream.Read(read, 0, 256); } resStream.Close(); } catch (Exception e) { strRet = ""; } return strRet; } /// <summary> /// 绑定Cookie、获取验证码 /// </summary> /// <param name="strUrl">链接地址(验证码获取地址)</param> /// <param name="server">Referer地址</param> /// <returns>Stream:验证码的数据流</returns> public static Stream GetModelStream(string strUrl, string server)//, string proxy, bool isProxy) { System.IO.Stream resStream = null; string cookie; try { HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); httpRequest.Timeout = 2000; httpRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; httpRequest.Referer = server; httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)"; HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); cookie = httpResponse.Headers.Get("Set-Cookie"); myCookie = new CookieContainer(); myCookie.SetCookies(new Uri(strUrl), cookie); resStream = httpResponse.GetResponseStream(); } catch { } return resStream; } /// <summary> /// Post方法提交 /// </summary> /// <param name="strUrl">目的资源链接</param> /// <param name="strParm">传递的参数和值</param> /// <returns>String:Response返回值</returns> public static string PostModel(string strUrl, string server, string strParm) { Encoding encode = System.Text.Encoding.UTF8; byte[] arrB = encode.GetBytes(strParm); HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl); myReq.Method = "POST"; myReq.ContentType = "application/x-www-form-urlencoded"; myReq.Timeout = 2000; myReq.Referer = server; myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)"; myReq.ContentLength = arrB.Length; myReq.CookieContainer = myCookie; Stream outStream = myReq.GetRequestStream(); outStream.Write(arrB, 0, arrB.Length); outStream.Close(); WebResponse myResp = null; try { //接收HTTP做出的响应 myResp = myReq.GetResponse(); } catch (Exception e) { } Stream ReceiveStream = myResp.GetResponseStream(); StreamReader readStream = new StreamReader(ReceiveStream, encode); Char[] read = new Char[256]; int count = readStream.Read(read, 0, 256); string str = null; while (count > 0) { str += new String(read, 0, count); count = readStream.Read(read, 0, 256); } readStream.Close(); myResp.Close(); return str; } } }
相关文章推荐
- 命名空间“System.Web”中不存在类型或命名空间名称“HttpUtility”。是否缺少程序集引用?
- 【OTT】【IPTV】三大通信运营商2016年IPTV、OTT招标一览
- php通过http_user_agent判断用户设备类型(脑残版)
- CentOS故障排除详解(3): 网络环境
- 利用String类制作简单的网络爬虫
- linux的网络配置和传输文件
- HTTP 返回状态代码
- https证书
- rtsp over http
- QTcpSocket 发送和接收数据的几种方法
- java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
- Linux(CentOS)安装完成后连接网络
- tomcat配置问题:访问http://localhost:8080/ 遇到 Access Error: 404
- nmap常用命令及参数说明
- 改变您的HTTP服务器的缺省banner
- vmware下 ubuntu 16.04.1 设置静态ip地址
- Linux-ubuntu 日记( 1 )- 传输文件+连接网络
- 线性规划与网络流24——运输问题
- api version最佳实践
- 线性规划与网络流24——最长k可重区间集问题