public static string Request(HttpVerbConst method, string url, Dictionary <string, string> data, WebHeaderCollection headers) { string str = ""; if (data == null) { str = null; } else { foreach (var param in data) { if (str != "") { str = str + "&"; } str += param.Key.UrlEncode() + "=" + param.Value.UrlEncode(); } } return(Request(method, url, str, headers)); }
/// <summary> /// Call a URL with any method (eg GET, PUT, POST, DELETE). /// Pass in any extra headers you want. /// To set UserAgent and Accept headers, set them in the headers collection and then we automatically pull these off and put them in the correct places. /// The data can be URL encoded or JSON or XML. /// We auto detect and send correct content-type header eg /// eg containerText=NYKU3012540&isImport=true will go as application/x-www-form-urlencoded /// or {"containerText":"NYKU3012540","isImport":true} will go as application/json /// Returns a string. /// </summary> public static string Request(HttpVerbConst method, string url, object data, WebHeaderCollection headers) { var ErrorFormat = "throw"; string dataString = null; if (data != null) { if (data is string) { dataString = (string)data; } else if (data is Dictionary <string, string> ) { foreach (var param in data as Dictionary <string, string> ) { if (dataString != null) { dataString = dataString + "&"; } dataString += param.Key.UrlEncode() + "=" + param.Value.UrlEncode(); } } else { throw new ProgrammingErrorException("Http.Request - Invalid data type. Use a string or a dictionary of strings."); } } if (method == Http.GET && dataString != null) { if (url.Contains("?")) { url += "&" + dataString; } else { url += "?" + dataString; } dataString = null; } HttpWebRequest webReq; try { webReq = WebRequest.Create(url) as HttpWebRequest; } catch (UriFormatException exception) { throw new ProgrammingErrorException("Incorrect URL format, you must use a absolute full URL. URL is: " + url, exception); } if (Util.GetSettingBool("SendRemoteRequestsViaFiddler", false)) { // ignore ssl cert errors - important for fiddler to capture https traffic webReq.Proxy = new WebProxy("http://127.0.0.1:8888", false); ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; } if (headers != null) { // To set UserAgent and Accept headers, set them in the headers collection and then we automatically pull these off and put them in the correct places // there could be other headers like this, so if you see it saying you can't set it, use this same approach if (headers.AllKeys.Contains("Accept")) { var accept = headers["Accept"]; headers.Remove("Accept"); webReq.Accept = accept; } if (headers.AllKeys.Contains("User-Agent")) { var userAgent = headers["User-Agent"]; headers.Remove("User-Agent"); webReq.UserAgent = userAgent; } if (headers.AllKeys.Contains("Referer")) //20140611 jn added { var referer = headers["Referer"]; headers.Remove("Referer"); webReq.Referer = referer; } if (headers.AllKeys.Contains("Connection")) //20140611 jn added { var connection = headers["Connection"]; headers.Remove("Connection"); webReq.Connection = connection; } if (headers.AllKeys.Contains("Content-Type")) //20140611 jn added { var contentType = headers["Content-Type"]; headers.Remove("Content-Type"); webReq.ContentType = contentType; } foreach (var header in headers.AllKeys) { try { webReq.Headers[header] = headers[header]; //20140611 jn put in catch } catch (ArgumentException exception) { throw new ProgrammingErrorException("header failed[" + header + "]", exception); } } //webReq.Headers = headers; headers.Remove("Host"); } webReq.Method = method.ToString().ToUpper(); if (dataString != null) // data will be null for a GET, otherwise should not be null { byte[] reqBytes = null; reqBytes = DefaultEncoding.GetBytes(dataString); if (webReq.ContentType == null) //leave this if already set by headers collection { if (dataString != null && dataString.StartsWith("{")) { webReq.ContentType = "application/json; charset=utf-8"; } else if (dataString != null && dataString.StartsWith("<?xml")) { webReq.ContentType = "application/xml; charset=utf-8"; } else { webReq.ContentType = "application/x-www-form-urlencoded"; } } webReq.ContentLength = reqBytes.Length; try { using (Stream requestStream = webReq.GetRequestStream()) { requestStream.Write(reqBytes, 0, reqBytes.Length); requestStream.Close(); } } catch (Exception e) { if (e.InnerException != null && e.InnerException.Message == "No connection could be made because the target machine actively refused it 127.0.0.1:8888") { throw new ProgrammingErrorException("Trying to proxy via Fiddler but Fiddler is not running. Use appsetting SendRemoteRequestsViaFiddlerDEV to turn on or off sending to Fiddler proxy.", e); } else { throw; } } } string result = null; HttpWebResponse webResponse = null; //try { webResponse = (HttpWebResponse)webReq.GetResponse(); // AF20140909: Removed notify because we want to handle Http Status codes in the calling method // MN breaking change but should be fine as it should fall through to notify anyway //} catch (Exception e) { // // try and replace the URL if we find it - if not, just return the user's Error String // Error.Notify(false, "", e); //} if (webResponse != null) { using (Stream responseStream = webResponse.GetResponseStream()) { if (responseStream != null) { using (StreamReader streamReader = new StreamReader(responseStream, DefaultEncoding)) { result = streamReader.ReadToEnd(); streamReader.Close(); } } } } return(result); }