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