Using C # for easy HTTP Transport

Source: Internet
Author: User

Use the simplest method to implement HTTP transfer return fixed value

 Public Partial classForm1:form {StaticSocket _socket =NewSockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);//Listening for sockets         PublicForm1 () {InitializeComponent (); }        Private voidWebbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) {            stringc = Cookiereader.getcookie ("http://10.33.208.230/uac/");  This. TextBox1.Text =C; Program.cookie=C;  This. Text ="Service side:"+WebBrowser1.Document.Title; }        Private voidForm1_shown (Objectsender, EventArgs e) {_socket. Bind (NewIPEndPoint (Ipaddress.any,8123)); _socket. Listen ( -); _socket. BeginAccept (NewAsyncCallback (onaccept), _socket); }        /// <summary>        ///accept requests to process HTTP/// </summary>        /// <param name= "ar" ></param>        Static voidonaccept (IAsyncResult ar) {Try{Socket Socket= Ar. AsyncState asSocket; Socket web_client= socket. Endaccept (AR);//receive the proxy socket from the browser//The most concurrent processing of HTTP requestsSocket. BeginAccept (NewAsyncCallback (onaccept), socket);//start the next HTTP request receive (when this line of code is placed at No.2, the HTTP request is processed serially, and the previous process blocks the next request processing)                byte[] Recv_buffer =New byte[1024x768*640]; intRecv_count = web_client. Receive (Recv_buffer);//receiving request data from the browser                stringRecv_request = Encoding.UTF8.GetString (Recv_buffer,0, Recv_count); //Resolve (Recv_request, web_client); //parsing, routing, processing                stringContent =Program.cookie; byte[] Cont =Encoding.UTF8.GetBytes (content);                Sendpagecontent (cont, web_client); //No.2 serial processing of HTTP requests            }            Catch(Exception ex) {//Writelog ("exception occurred while processing HTTP request! "+ Environment.NewLine +" \ T "+ ex. Message);            }        }        Static voidSendpagecontent (byte[] pagecontent, Socket response) {            stringStatusline ="http/1.1 ok\r\n";//Status Line            byte[] Statusline_to_bytes =Encoding.UTF8.GetBytes (Statusline); byte[] Content_to_bytes =PageContent; stringHeader =string. Format ("content-type:text/html;charset=utf-8\r\ncontent-length:{0}\r\n", Content_to_bytes.            Length); byte[] header_to_bytes = Encoding.UTF8.GetBytes (header);//Answer HeaderResponse.  Send (statusline_to_bytes); //Send status lineResponse. Send (header_to_bytes);//Send Answer HeaderResponse. Send (New byte[] { (byte)'\ r', (byte)'\ n'});//Send blank lineResponse. Send (content_to_bytes);//send body (HTML)Response.        Close (); }    }

Using C # for easy HTTP Transport

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.