Esempio n. 1
0
        public string GetResponseByHttps(RequestParameter connectionInfo)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(connectionInfo.Url);
                request.Proxy            = null;
                request.Method           = connectionInfo.Method.ToUpper();
                request.ContentType      = connectionInfo.ContentType;
                request.Timeout          = Timeout.Infinite;
                request.ReadWriteTimeout = Timeout.Infinite;
                request.ServicePoint.Expect100Continue = false;

                if (connectionInfo.header.Count > 0)
                {
                    foreach (var kv in connectionInfo.header)
                    {
                        request.Headers.Add(kv.Key, kv.Value);
                    }
                }

                if (connectionInfo.Method.ToUpper() == "POST")
                {
                    var byteArray = Encoding.UTF8.GetBytes(connectionInfo.PostMessage);

                    request.ContentLength = byteArray.Length;

                    using (var dataStream = request.GetRequestStream())
                    {
                        dataStream.Write(byteArray, 0, byteArray.Length);
                    }
                }


                string resultString = string.Empty;
                using (var response = request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        if (stream == null)
                        {
                            Console.WriteLine("[Get Response By Https Error]", "Response data is null.");
                        }
                        else
                        {
                            using (var streamReader = new StreamReader(stream, connectionInfo.GetEncoding()))
                            {
                                resultString = streamReader.ReadToEnd();
                            }
                        }
                    }
                }
                request = null;
                return(resultString);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(null);
            }
        }
Esempio n. 2
0
        public string GetAsyncResponseByHttps(RequestParameter connectionInfo, AsyncCallback resultCallback)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(connectionInfo.Url);
                request.PreAuthenticate = true;
                request.Proxy           = null;
                request.KeepAlive       = false;
                request.Method          = connectionInfo.Method.ToUpper();
                request.ContentType     = connectionInfo.ContentType;
                request.Timeout         = Timeout.Infinite;
                request.ServicePoint.UseNagleAlgorithm = false;
                request.ServicePoint.Expect100Continue = false;
                request.ServicePoint.ConnectionLimit   = 1000;

                //var cash = new CredentialCache();
                //cash.Add(new Uri(connectionInfo.Url), "Basic", new NetworkCredential("n3ncollect", "welcome"));
                //request.Credentials = cash;

                if (connectionInfo.Method.ToUpper() == "POST")
                {
                    var byteArray = Encoding.UTF8.GetBytes(connectionInfo.PostMessage);

                    request.ContentLength = byteArray.Length;

                    using (var dataStream = request.GetRequestStream())
                    {
                        dataStream.Write(byteArray, 0, byteArray.Length);
                    }
                }

                var reqState = new RequestState();

                reqState.Request = request;

                request.BeginGetResponse(resultCallback == null ? resultCallback : RespCallbackForString, reqState);

                return(reqState.responseString);
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.ToString());
                return(null);
            }
        }