/// <summary> /// Invokes the req against a remote server. /// </summary> /// <param name="req"></param> /// <param name="readBytes"></param> /// <returns></returns> public RESTResult InvokeRequest(RESTRequest req, bool readBytes) { // If encrypt entity data add that header. if (req.EncryptEntityData) { req.Headers["X-Encrypted-Entity-Data"] = "true"; } // If encrypt entity data add that header. if (req.EncryptQueryData) { req.Headers["X-Encrypted-Query-Data"] = "true"; } string timestamp = RESTUtil.GetTimestamp(); String signatureData = req.HttpMethod + ' ' + this.getResourceURI(req) + ' ' + timestamp; req.Headers["X-Timestamp"] = timestamp; req.Headers["X-ApiKey"] = _apiKey; req.Headers["X-Signature"] = RESTUtil.SignString(signatureData, _secret); RESTResult result = new RESTResult(); result = getPostResponse(req, readBytes); return result; }
private RESTResult getPostResponse(RESTRequest req, bool readRaw) { RESTResult result = new RESTResult(); HttpWebRequest request = WebRequest.Create(this.getResourceURI(req)) as HttpWebRequest; request.AllowAutoRedirect = false; request.Headers.Add(req.Headers); request.UserAgent = this.UserAgent; request.Method = req.HttpMethod; request.Timeout = 50000; if (req.HttpMethod == "POST" && req.EntityData == null) { req.EntityData = ""; } if (req.HttpMethod == "POST" && req.EntityData != null) { string data = req.EntityData; if (req.EncryptEntityData) { data = RESTUtil.EncryptString(data, _apiKey, _secret); } byte[] requestData = new UTF8Encoding().GetBytes(data); request.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; request.ContentLength = requestData.Length; /* Submit the request to the body */ using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(requestData, 0, requestData.Length); requestStream.Close(); } } try { using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse) { result.setStatusCode(httpResponse.StatusCode); result.setHeaders(httpResponse.Headers as NameValueCollection); using(Stream response = httpResponse.GetResponseStream()) { if (readRaw) { int len = (int)httpResponse.ContentLength; byte[] respRaw = new byte[len]; response.Read(respRaw, 0, len); result.setResponseRaw(respRaw); } if (!readRaw) { using (StreamReader reader = new StreamReader(response, Encoding.UTF8)) { result.setResponseText(reader.ReadToEnd()); if (result.Headers["X-Encrypted"] == "true") { result.setResponseText(RESTUtil.DecryptString(result.ResponseText, _apiKey, _secret)); } reader.Close(); } } httpResponse.Close(); } } } catch (WebException webEx) { if (webEx.Response != null) { using (HttpWebResponse errorResponse = webEx.Response as HttpWebResponse) { result.setStatusCode(errorResponse.StatusCode); result.setHeaders(errorResponse.Headers as NameValueCollection); using (StreamReader reader = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8)) { result.setResponseText(reader.ReadToEnd()); if (result.Headers["X-Encrypted"] == "true") { result.setResponseText(RESTUtil.DecryptString(result.ResponseText, _apiKey, _secret)); } reader.Close(); } } } else { result.setStatusCode(HttpStatusCode.BadRequest); result.setResponseText(webEx.Message); } } return result; }
private string getResourceURI(RESTRequest req) { String resourceURI = this.BaseURI + this.Resource; String query = req.GetQueryString(); if (!String.IsNullOrEmpty(query)) { if (req.EncryptQueryData) { query = HttpUtility.UrlEncode( RESTUtil.EncryptString(query, _apiKey, _secret), Encoding.UTF8); } resourceURI += '?' + query; } return resourceURI; }
/// <summary> /// Invokes a web service req. /// </summary> /// <param name="req"></param> /// <returns></returns> public RESTResult InvokeRequest(RESTRequest req) { return InvokeRequest(req, false); }