Exemplo n.º 1
0
        protected static TestResponse Send(Uri url, string method, byte[] contentBytes = null, string contentType = null, NameValueCollection headers = null)
        {
            try
            {
                var request = WebRequest.Create(url);
                request.Method = method;

                if (headers != null)
                {
                    foreach (var headerName in headers.AllKeys)
                    {
                        request.Headers.Add(headerName, headers[headerName]);
                    }
                }

                if (contentBytes != null && contentBytes.Length > 0)
                {
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(contentBytes, 0, contentBytes.Length);
                    }
                    if (contentType != null)
                    {
                        request.ContentType = contentType;
                    }
                }

                request.Timeout = 2000;

                var httpWebResponse = (HttpWebResponse)request.GetResponse();
                return(Convert(httpWebResponse));
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    return(Convert((HttpWebResponse)e.Response));
                }

                return(TestResponse.Create(452));
            }
        }
Exemplo n.º 2
0
 private static TestResponse Convert(HttpWebResponse httpWebResponse)
 {
     return(TestResponse.Create((int)httpWebResponse.StatusCode, ReadResponseContentBytes(httpWebResponse)));
 }