美文网首页
HttpWebRequest异步方式

HttpWebRequest异步方式

作者: 泱千澈 | 来源:发表于2020-03-07 12:23 被阅读0次

这篇其实是对简体翻译成繁体(三)的一个补充

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace ConsoleApp1
{
    public class ChineseConvert
    {
        public static ManualResetEvent allDone = new ManualResetEvent(false);
        public static void GetWebString(string cc)
        {
            HttpWebRequest request = null;
            const string url = "https://brushes8.com/zhong-wen-jian-ti-fan-ti-zhuan-huan"; 
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*;q=0.8,application/signed-exchange;v=b3";
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = true;
            request.KeepAlive = true;//建立持久性连接
            string postData = string.Format("data={0}&variant=zh-tw&dochineseconversion=1&submit=开始转换 (Ctrl + Enter)", cc);
            //postData = System.Web.HttpUtility.UrlEncode(postData);
            //ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            request.BeginGetRequestStream(WriteCallback, new RequestState(request, byteArray));
            allDone.WaitOne();
        }

        private static void WriteCallback(IAsyncResult asynchronousResult)
        {
            RequestState state = (RequestState)asynchronousResult.AsyncState;
            byte[] postData = state.postData;
            HttpWebRequest request = state.request;

            // End the operation.
            Stream postStream = request.EndGetRequestStream(asynchronousResult);
            // Convert the string into a byte array.
            // Write to the request stream.
            postStream.Write(postData, 0, postData.Length);

            //响应
            request.BeginGetResponse(ReadCallback, state);
            allDone.Set();
            postStream.Close();
        }

        private static void ReadCallback(IAsyncResult asynchronousResult)
        {
            RequestState state = (RequestState)asynchronousResult.AsyncState;
            HttpWebRequest request = state.request;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            string text = string.Empty;
            using (Stream responseStm = response.GetResponseStream())
            {
                using (System.IO.StreamReader redStm = new StreamReader(responseStm, Encoding.UTF8))
                {
                    text = redStm.ReadToEnd();
                }
            }

            // int idx = text.LastIndexOf(">");
            text = text.Substring(14457);
        }
    }

    public class RequestState
    {
        public HttpWebRequest request;
        public byte[] postData;

        public RequestState(HttpWebRequest request, byte[] postData)
        {
            this.request = request;
            this.postData = postData;
        }
    }
}

相关文章

网友评论

      本文标题:HttpWebRequest异步方式

      本文链接:https://www.haomeiwen.com/subject/qwiqdhtx.html