Http 协议数据传输.doc
《Http 协议数据传输.doc》由会员分享,可在线阅读,更多相关《Http 协议数据传输.doc(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流Http 协议数据传输【精品文档】第 19 页1using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;using System.Collections;using System.IO;using System.Text.RegularExpressions;using RE = System.Text.RegularExpressions.Regex;using System.Security.Cr
2、yptography.X509Certificates;/* *文件名:HttpProc.cs* *创建人:HeDaode* *日 期:2007.09.01* *描 述:实现HTTP协议中的GET、POST请求* *使 用:HttpProc.WebClient client = new HttpProc.WebClient(); client.Encoding = System.Text.Encoding.Default;/默认编码方式,根据需要设置其他类型 client.OpenRead(普通get请求 MessageBox.Show(client.RespHtml);/获取返回的网页源代码
3、 client.DownloadFile(下载文件 client.OpenRead(提交表单,此处是登录百度的示例 client.UploadFile(, file1=D:1.mp3);/上传文件 client.UploadFile(, folder=myfolder&size=4003550,file1=D:1.mp3);/提交含文本域和文件域的表单*/namespace HttpProc / /上传事件委托 / / / public delegate void WebClientUploadEvent(object sender, HttpProc.UploadEventArgs e);
4、/ /下载事件委托 / / / public delegate void WebClientDownloadEvent(object sender, HttpProc.DownloadEventArgs e); / /上传事件参数 / public struct UploadEventArgs / /上传数据总大小 / public long totalBytes; / /已发数据大小 / public long bytesSent; / /发送进度(0-1) / public double sendProgress; / /发送速度Bytes/s / public double sendSp
5、eed; / /下载事件参数 / public struct DownloadEventArgs / /下载数据总大小 / public long totalBytes; / /已接收数据大小 / public long bytesReceived; / /接收数据进度(0-1) / public double ReceiveProgress; / /当前缓冲区数据 / public byte receivedBuffer; / /接收速度Bytes/s / public double receiveSpeed; / /实现向WEB服务器发送和接收数据 / public class WebCl
6、ient private WebHeaderCollection requestHeaders, responseHeaders; private TcpClient clientSocket; private MemoryStream postStream; private Encoding encoding = Encoding.Default; private const string BOUNDARY = -HEDAODE-; private const int SEND_BUFFER_SIZE = 10245; private const int RECEIVE_BUFFER_SIZ
7、E = 10245; private string cookie = ; private string respHtml = ; private string strRequestHeaders = ; private string strResponseHeaders = ; private int statusCode = 0; private bool isCanceled = false; public event WebClientUploadEvent UploadProgressChanged; public event WebClientDownloadEvent Downlo
8、adProgressChanged; / /初始化WebClient类 / public WebClient() responseHeaders = new WebHeaderCollection(); requestHeaders = new WebHeaderCollection(); / /读取指定URL的文本 / /请求的地址 /服务器响应文本 public string OpenRead(string URL) requestHeaders.Add(Connection, close); SendRequestData(URL, GET); return GetHtml(); /解决
9、证书过期无法访问的问题 class CertPolicy : ICertificatePolicy public bool CheckValidationResult(ServicePoint srvpt, X509Certificate cert, WebRequest req, int certprb) return true; / /采用https协议访问网络 / /url地址 /发送的数据 / public string OpenReadWithHttps(string URL,string strPostdata) ServicePointManager.CertificatePol
10、icy = new CertPolicy(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.CookieContainer = new CookieContainer(); request.Method = POST; request.Accept = */*; request.ContentType=application/x-www-form-urlencoded; byte buffer = this.encoding.GetBytes(strPostdata); request.Con
11、tentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), encoding); this.respHtml = reader.ReadToEnd(); foreach (System.Net.Cookie ck
12、 in response.Cookies) this.cookie += ck.Name + = + ck.Value + ; reader.Close(); return respHtml; / /读取指定URL的文本 / /请求的地址 /向服务器发送的文本数据 /服务器响应文本 public string OpenRead(string URL, string postData) byte sendBytes = encoding.GetBytes(postData); postStream = new MemoryStream(); postStream.Write(sendBytes,
13、 0, sendBytes.Length); requestHeaders.Add(Content-Length, postStream.Length.ToString(); requestHeaders.Add(Content-Type, application/x-www-form-urlencoded); requestHeaders.Add(Connection, close); SendRequestData(URL, POST); return GetHtml(); / /读取指定URL的流 / /请求的地址 /向服务器发送的数据 /服务器响应流 public Stream Get
14、Stream(string URL, string postData) byte sendBytes = encoding.GetBytes(postData); postStream = new MemoryStream(); postStream.Write(sendBytes, 0, sendBytes.Length); requestHeaders.Add(Content-Length, postStream.Length.ToString(); requestHeaders.Add(Content-Type, application/x-www-form-urlencoded); r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Http 协议数据传输 协议 数据传输
限制150内