コード例 #1
0
ファイル: ClientTest.aspx.cs プロジェクト: iorilan/Samples
        /// <summary>
        /// 异步发送POST请求
        /// </summary>
        /// <typeparam name="strRequestXml">要发送的XML字符串</typeparam>
        /// <param name="url">url</param>
        /// <param name="OnResponseGet"></param>
        public void AysncPostRequest(string strRequestXml, string url, int timeout, Action <string, Exception> OnResponseGet)
        {
            try
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[]       data     = encoding.GetBytes(strRequestXml);

                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                myHttpWebRequest.Timeout       = timeout;
                myHttpWebRequest.Method        = "POST";
                myHttpWebRequest.ContentType   = "application/json;charset=utf-8";
                myHttpWebRequest.ContentLength = data.Length;
                // myHttpWebRequest.Headers.Add("Authorization", strAuth);
                myHttpWebRequest.CookieContainer = new CookieContainer();
                CookieContainer cookies = myHttpWebRequest.CookieContainer; //保存cookies
                cookies.Add(myHttpWebRequest.RequestUri, new Cookie("ssic", txtSSIC.Text));
                // cookies.Add(myHttpWebRequest.RequestUri, new Cookie("domain", txtDomain.Text.Trim()));
                myHttpWebRequest.CookieContainer = cookies;
                myHttpWebRequest.Headers.Add("ssic", txtSSIC.Text);


                AsynHttpContext asynContext = new AsynHttpContext(myHttpWebRequest);
                string          tranKey     = TransactionManager <AsynHttpContext> .Instance.Register(asynContext);

                AysncHttpRequestHelperV2.Post(asynContext, data, new Action <AsynHttpContext, Exception>((httpContext, ex) =>
                {
                    TransactionManager <AsynHttpContext> .Instance.Unregister(tranKey);
                    if (ex != null)
                    {
                        OnResponseGet(null, ex);
                    }
                    else
                    {
                        string rspStr = Encoding.UTF8.GetString(httpContext.ResponseBytes);
                        OnResponseGet(rspStr, null);
                    }
                }));
            }
            catch (Exception ex)
            {
                OnResponseGet(null, ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// 异步发送POST请求
        /// </summary>
        /// <typeparam name="strRequestXml">要发送的XML字符串</typeparam>
        /// <param name="url">url</param>
        /// <param name="OnResponseGet"></param>
        public static void AysncPostRequest(string strRequestXml, string url, string strAuth, int timeout, Action <string, Exception> OnResponseGet)
        {
            try
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[]       data     = encoding.GetBytes(strRequestXml);

                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                myHttpWebRequest.Timeout       = timeout;
                myHttpWebRequest.Method        = "POST";
                myHttpWebRequest.ContentType   = "application/x-www-form-urlencoded";
                myHttpWebRequest.ContentLength = data.Length;
                myHttpWebRequest.Headers.Add("Authorization", strAuth);

                AsynHttpContext asynContext = new AsynHttpContext(myHttpWebRequest);
                string          tranKey     = TransactionManager <AsynHttpContext> .Instance.Register(asynContext);

                AysncHttpRequestHelperV2.Post(asynContext, data, new Action <AsynHttpContext, Exception>((httpContext, ex) =>
                {
                    TransactionManager <AsynHttpContext> .Instance.Unregister(tranKey);
                    if (ex != null)
                    {
                        OnResponseGet(null, ex);
                    }
                    else
                    {
                        string rspStr = Encoding.UTF8.GetString(httpContext.ResponseBytes);
                        OnResponseGet(rspStr, null);
                    }
                }));
            }
            catch (Exception ex)
            {
                OnResponseGet(null, ex);
            }
        }