C # Implementation HTTP protocol support upload download file get, POST request

Source: Internet
Author: User
Tags rar
C # Implementation HTTP protocol support upload download file get, POST request
Using 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.Cryptography.X509Certificates;  /********************************************************************************************************  * * FileName: HttpProc.cs  * * Creator: Hedaode  * * Day   Period: 2007.09.01  * * Tracing   Description: Implementing GET, POST request in HTTP protocol  * *  : httpproc.webclient client = new Httpproc.webclient ();             client. Encoding = system.text.encoding.default;//default encoding, set other types as needed              client. OpenRead ("http://www.baidu.com");/General GET request             MessageBox.Show (client. resphtml);//Get ReturnBack to the Web page source code             client. DownloadFile ("Http://www.loopc.com/upload/163album.rar", @ "C:/163album.rar");//download file              client. OpenRead ("Http://passport.baidu.com/?login", "username=zhangsan&password=123456")//Submit form, here is an example of login to Baidu              client. UploadFile ("Http://hiup.baidu.com/zhangsan/upload", @ "File1=d:/1.mp3");//Upload file              client. UploadFile ("Http://hiup.baidu.com/zhangsan/upload", "folder=myfolder&size=4003550", @ "File1=d:/1.mp3"); Submit forms with text fields and file fields ************************************************************************************************** /  namespace Httpproc {   ///<summary >    ///Upload event delegate    ///</summary>    /<param name= "Sender" ></param>    ///<param name= "E" ></param>      Public delegate void Webclientuploadevent (object sender, Httpproc.uploadeventargs e);      ///<summary>    ///Download event delegate    ///</summary>    ///<param name= "sender" ></param>    ///<param name= "E" ></ param>     public delegate void Webclientdownloadevent (object sender, Httpproc.downloadeventargs e);        ///<summary>    ///upload event parameter    ///</summary >     public struct Uploadeventargs     {        <summary>        ///upload data total size         ///</summary>         public long totalbytes;        ///<summary>        ///data size         ///</summary>         Public long BytesSent;        ///<summary>        ///Send Progress ( 0-1)        ///</summary>         public double sendprogress;        ///<summary>        /// Send Speed bytes/s        ///</summary>          public double sendspeed;          ///<summary>    ///Download event parameters    ///& lt;/summary>     public struct Downloadeventargs     {        ///<summary>        ///Download Data total size        /// </summary>         public long totalbytes;        ///<summary>        /// Received data size        ///</summary>         public long bytesreceived;        ///<summary>        /// Receive data Progress (0-1)        ///</summary>          public double receiveprogress;        ///<summary>        /// Current buffer data        ///</summary>         Public byte[] Receivedbuffer;        <summary>        ///receiving speed bytes/s         ///</summary>         public double receivespeed;    }      ///<summary>    ///Implementation send and receive data to Web server     ///</summary>     public class WebClient     {         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_size = 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 downloadprogresschanged;          ///<summary>        // /initialization of WebClient        ///</summary>          public WebClient ()         {&NBSP;&NBsp;          responseheaders = new WebHeaderCollection ();             requestheaders = new WebHeaderCollection ();        }            ///< Summary>        ///reads the text of the specified URL        // /</summary>        ///<param name= "URL" > Requested address </param>         ///<returns> Server response text </returns>          public String OpenRead (string URL)         {             Requestheaders.add ("Connection", "close");             Sendrequestdata (URL, "get");             return gethtml ();        }            // Troubleshoot issues that cannot be accessed by certificate expiration         class Certpolicy:icertificatepolicy          {            public bool CheckValidationResult (ServicePoint srvpt, X509Certificate cert, WebRequest req, int certprb)     & nbsp;       {return true;}        }    & nbsp;     ///<summary>        /// Use HTTPS protocol to access network        ///</summary>         ///<param name= "URL" >url address </param>        ///<param name= "Strpostdata" > Data sent </param>        ///<returns></returns>         public string Openreadwithhttps (String url,string strpostdata)         {             servicepointmanager.certificatepolicy = 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);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.