/// <summary> /// Perform HTTP request. /// </summary> /// <param name="request"></param> /// <param name="httpWebRequest">Custom HttpWebRequest object to use</param> /// <returns></returns> public static Energy.Base.Http.Response Execute(Energy.Base.Http.Request request, HttpWebRequest httpWebRequest) { HttpWebRequest httpRequest = null; //if (request.RequestObject != null) // httpRequest = (HttpWebRequest)WebRequest.Create(request.Url); //else // httpRequest = (HttpWebRequest)WebRequest.Create(request.Url); if (httpWebRequest != null) { httpRequest = httpWebRequest; } else { httpRequest = (HttpWebRequest)WebRequest.Create(request.Url); } httpRequest.Method = request.Method; if (!string.IsNullOrEmpty(request.ContentType)) { httpRequest.ContentType = request.ContentType; } if (!string.IsNullOrEmpty(request.AcceptType)) { httpRequest.Accept = request.AcceptType; } if (request.Headers != null && request.Headers.Count > 0) { for (int i = 0; i < request.Headers.Count; i++) { httpRequest.Headers.Add(request.Headers[i]); } } Energy.Base.Http.Response response = new Base.Http.Response(); int statusCode = 0; //responseHeaders = null; byte[] responseData = null; byte[] data = request.Data; if (data != null && data.Length > 0) { httpRequest.ContentLength = data.Length; using (Stream requestStream = httpRequest.GetRequestStream()) { if (requestStream == null) { return(response); } requestStream.Write(data, 0, data.Length); requestStream.Flush(); requestStream.Close(); } } using (HttpWebResponse httpResponse = (HttpWebResponse)GetResponseWithoutException(httpRequest)) { //response.ResponseObject = httpResponse; statusCode = (int)httpResponse.StatusCode; response.StatusCode = statusCode; if (httpResponse.Headers.Count > 0) { int responseHeadersCount = httpResponse.Headers.Count; string[] responseHeaders = new string[httpResponse.Headers.Count]; string[] keyArray = httpResponse.Headers.AllKeys; for (int i = 0; i < responseHeadersCount; i++) { responseHeaders[i] = string.Concat(keyArray[i], ": ", httpResponse.Headers[i]); } response.Headers.AddRange(responseHeaders); //Energy.Base.Collection.StringDictionary d = new Energy.Base.Collection.StringDictionary(); //List<string> headerList = new List<string>(); //string[] keys = httpResponse.Headers.AllKeys; //for (int i = 0; i < keys.Length; i++) //{ // //d[keys[i]] = httpResponse.Headers[i]; // headerList.Add(keys[i] + ": " + httpResponse.Headers[i]); //} //response.Headers = headerList; ////responseHeaders = d.ToArray(); } using (Stream responseStream = httpResponse.GetResponseStream()) { if (responseStream != null) { using (Energy.Base.ByteArrayBuilder builder = new Energy.Base.ByteArrayBuilder(responseStream)) { responseData = builder.ToArray(); } } response.Data = responseData; return(response); } } }
public static int Request(string method, string url, byte[] data , string contentType , string acceptType , string[] requestHeaders , out string[] responseHeaders , out byte[] responseData ) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; if (!string.IsNullOrEmpty(contentType)) { request.ContentType = contentType; } if (!string.IsNullOrEmpty(acceptType)) { request.Accept = acceptType; } if (requestHeaders != null && requestHeaders.Length > 0) { //for (int i = 0; i < requestHeaders.Length / 2; i++) //{ // request.Headers.Add(requestHeaders[i], requestHeaders[i + 1]); //} for (int i = 0; i < requestHeaders.Length; i++) { request.Headers.Add(requestHeaders[i]); } } int statusCode = 0; responseHeaders = null; responseData = null; if (data != null && data.Length > 0) { request.ContentLength = data.Length; using (Stream requestStream = request.GetRequestStream()) { if (requestStream == null) { return(0); } requestStream.Write(data, 0, data.Length); requestStream.Flush(); requestStream.Close(); } } using (HttpWebResponse response = (HttpWebResponse)GetResponseWithoutException(request)) { statusCode = (int)response.StatusCode; if (response.Headers.Count > 0) { Energy.Base.Collection.StringDictionary d = new Energy.Base.Collection.StringDictionary(); string[] keys = response.Headers.AllKeys; for (int i = 0; i < keys.Length; i++) { d[keys[i]] = response.Headers[i]; } responseHeaders = d.ToArray(); } using (Stream responseStream = response.GetResponseStream()) { if (responseStream != null) { using (Energy.Base.ByteArrayBuilder builder = new Energy.Base.ByteArrayBuilder(responseStream)) { responseData = builder.ToArray(); } } return(statusCode); } } }