public ClientResponse <RS, ERS> Go() { if (url.Length == 0) { throw new InvalidOperationException("You must specify a URL"); } if (method == HTTPMethod.UNSET) { throw new InvalidOperationException("You must specify a HTTP method"); } if (typeof(RS) != typeof(RESTVoid) && successResponseHandler == null) { throw new InvalidOperationException( "You specified a success response type, you must then provide a success response handler."); } if (typeof(ERS) != typeof(RESTVoid) && errorResponseHandler == null) { throw new InvalidOperationException( "You specified an error response type, you must then provide an error response handler."); } var response = new ClientResponse <RS, ERS>(); response.request = (bodyHandler != null) ? bodyHandler.GetBodyObject() : null; response.method = method; HttpWebRequest request; try { if (parameters.Count > 0) { if (!url.ToString().Contains("?")) { url.Append("?"); } foreach (var pair in parameters) { foreach (var value in pair.Value) { url.Append(HttpUtility.UrlEncode(pair.Key, Encoding.UTF8)).Append("=") .Append(HttpUtility.UrlEncode(value.ToString(), Encoding.UTF8)).Append("&"); } } url.Remove(url.Length - 1, 1); } response.url = new Uri(url.ToString()); request = (HttpWebRequest)WebRequest.Create(response.url); if (webProxy != null) { request.Proxy = webProxy; } // Handle SSL certificates if (response.url.Scheme.ToLower().Equals("https") && certificate != null) { ServicePointManager.CertificatePolicy = new CertPolicy(); request.ClientCertificates.Add(certificate); } request.Timeout = timeout; request.ReadWriteTimeout = readWriteTimeout; request.Method = method.ToString(); if (headers.Count > 0) { foreach (var header in headers) { request.Headers[header.Key] = header.Value; } } if (bodyHandler != null) { bodyHandler.SetHeaders(request); } if (bodyHandler != null) { using (var stream = request.GetRequestStream()) { bodyHandler.Accept(stream); stream.Flush(); } } } catch (Exception e) { logger.Debug("Error calling REST WebService at [" + url + "]", e); response.status = -1; response.exception = e; return(response); } try { using (var resp = (HttpWebResponse)request.GetResponse()) { response.status = (int)resp.StatusCode; if (successResponseHandler == null) { return(response); } try { using (var str = resp.GetResponseStream()) { response.successResponse = successResponseHandler.Apply(str); } } catch (Exception e) { logger.Debug("Error calling REST WebService at [" + url + "]", e); response.status = -1; response.exception = e; return(response); } } } catch (WebException e) { response.status = -1; // The response will be null if the server couldn't be contacted, the connection broke, or // the communication with the server failed if (e.Response == null) { response.exception = e; return(response); } using (var webResp = e.Response) { var httpResp = (HttpWebResponse)webResp; response.status = (int)httpResp.StatusCode; if (errorResponseHandler == null) { return(response); } try { using (var str = httpResp.GetResponseStream()) { response.errorResponse = errorResponseHandler.Apply(str); } } catch (Exception ex) { logger.Debug("Error calling REST WebService at [" + url + "]", e); response.exception = ex; return(response); } } } return(response); }