Пример #1
0
        public MemoryStream RequestMemoryStream(HttpRequestInformation information)
        {
            MemoryStream memoryStream = new MemoryStream();

            Request(null, information, memoryStream);
            return(memoryStream);
        }
Пример #2
0
        public MemoryStream RequestMemoryStream(string url, HttpRequestInformation information)
        {
            MemoryStream memoryStream = new MemoryStream();

            Request(url, information, memoryStream);
            return(memoryStream);
        }
Пример #3
0
 public byte[] RequestBytes(HttpRequestInformation information)
 {
     using (MemoryStream memoryStream = new MemoryStream()) {
         Request(null, information, memoryStream);
         return(memoryStream.ToArray());
     }
 }
Пример #4
0
 public string Request(HttpRequestInformation information)
 {
     using (MemoryStream memoryStream = new MemoryStream()) {
         Request(null, information, memoryStream);
         return(Encoding.GetString(memoryStream.ToArray()));
     }
 }
Пример #5
0
        public void Request(string url, HttpRequestInformation information, Stream responseStream)
        {
            if (url == null)
            {
                url = information == null ? string.Empty : information.Url;
            }
            StringBuilder stringBuilder = new StringBuilder();
            int           removeIndex   = -1;

            if (!url.StartsWith("http") && !string.IsNullOrEmpty(BaseURL))
            {
                stringBuilder.Append(BaseURL);
                if (!BaseURL.EndsWith("/"))
                {
                    stringBuilder.Append('/');
                }
                if (url.StartsWith("/"))
                {
                    removeIndex = stringBuilder.Length;
                }
            }
            stringBuilder.Append(url);
            if (removeIndex > -1)
            {
                stringBuilder.Remove(removeIndex, 1);
            }
            int  questionMarkIndex = url.IndexOf('?');
            bool hasParameter      = false;

            if (questionMarkIndex > -1)
            {
                if (questionMarkIndex == url.Length - 1)
                {
                    stringBuilder.Remove(questionMarkIndex, 1);
                }
                else
                {
                    hasParameter = true;
                }
            }
            if (information != null && information.QueryStringParameters != null && information.QueryStringParameters.Count > 0)
            {
                stringBuilder.Append(hasParameter ? '&' : '?');
                stringBuilder.Append(information.QueryStringParameters);
            }
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(stringBuilder.ToString());

            if (information != null)
            {
                if (information.FormData == null)
                {
                    httpWebRequest.Method = information.Type.ToString().ToUpperInvariant();
                }
                else
                {
                    httpWebRequest.Method = HttpRequestType.Post.ToString().ToUpperInvariant();
                }
            }
            httpWebRequest.CookieContainer = CookieContainer;
            if (OnRequest != null && !OnRequest(httpWebRequest))
            {
                return;
            }
            if (information != null && information.OnRequest != null && !information.OnRequest(httpWebRequest))
            {
                return;
            }
            byte[] buffer = new byte[BufferSize];
            if (information != null && information.FormData != null)
            {
                using (information.FormData) {
                    using (Stream stream = httpWebRequest.GetRequestStream()) {
                        while (true)
                        {
                            int length = information.FormData.Stream.Read(buffer, 0, buffer.Length);
                            if (information != null && information.OnRequesting != null && !information.OnRequesting(length))
                            {
                                break;
                            }
                            if (length < 1)
                            {
                                if (information != null && information.OnRequested != null)
                                {
                                    information.OnRequested();
                                }
                                break;
                            }
                            stream.Write(buffer, 0, length);
                        }
                    }
                }
            }
            HttpWebResponse httpWebResponse;

            try {
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            } catch (WebException webException) {
                httpWebResponse = webException.Response as HttpWebResponse;
                if (information == null || information.OnResponseError == null)
                {
                    if (httpWebResponse == null)
                    {
                        throw;
                    }
                }
                else
                {
                    if (OnResponseError != null && !OnResponseError(httpWebResponse, webException))
                    {
                        return;
                    }
                    if (information != null && information.OnResponseError != null && !information.OnResponseError(httpWebResponse, webException))
                    {
                        return;
                    }
                }
            }
            if (httpWebResponse == null)
            {
                return;
            }
            using (httpWebResponse) {
                if (OnResponse != null && !OnResponse(httpWebResponse))
                {
                    return;
                }
                if (information != null && information.OnResponse != null && !information.OnResponse(httpWebResponse))
                {
                    return;
                }
                using (Stream stream = httpWebResponse.GetResponseStream()) {
                    while (true)
                    {
                        int length = stream.Read(buffer, 0, buffer.Length);
                        if (information != null && information.OnResponding != null && !information.OnResponding(length))
                        {
                            break;
                        }
                        if (length == 0)
                        {
                            if (OnResponded != null)
                            {
                                OnResponded(responseStream);
                            }
                            responseStream.Position = 0;
                            if (information != null && information.OnResponded != null)
                            {
                                information.OnResponded(responseStream);
                            }
                            if (OnRespondedText != null || (information != null && information.OnRespondedText != null))
                            {
                                responseStream.Position = 0;
                                using (StreamReader streamReader = new StreamReader(responseStream, Encoding)) {
                                    string text = streamReader.ReadToEnd();
                                    if (OnRespondedText != null)
                                    {
                                        OnRespondedText(text);
                                    }
                                    if (information != null && information.OnRespondedText != null)
                                    {
                                        information.OnRespondedText(text);
                                    }
                                }
                            }
                            break;
                        }
                        responseStream.Write(buffer, 0, length);
                    }
                }
            }
        }
Пример #6
0
 public void Request(HttpRequestInformation information, Stream responseStream)
 {
     Request(null, information, responseStream);
 }