private WebMetodAnser GetWebServiceResponse(eWebMetods webMetod, List<KeyValuePair<string, string>> postParams) { WebMetodDescription webMetodDescription = new WebMetodDescription(); webMetodDescription.URI = webServices[(int)AuthMethod[(int)webMetod]]; webMetodDescription.AUTHConnection = AuthMethod[(int)webMetod]; webMetodDescription.PostData = PostParamsToPostData(postParams); WebMetodAnser webMetodAnser = GetWebResponse(webMetodDescription); if (webMetodAnser.Error != null || webMetodAnser.Status != "OK"){ webMetodAnser.StringReturn = null; } else{ try{ webMetodAnser.StringReturn = JObject.Parse(webMetodAnser.StringReturn)["return"].ToString(); } catch{ try{ webMetodAnser.Error = JObject.Parse(webMetodAnser.StringReturn)["error"].ToString(); } catch{ webMetodAnser.StringReturn = Regex.Replace( JObject.Parse(webMetodAnser.StringReturn)["moreinfo"].ToString(), "<[^>]+>", string.Empty); } } } return webMetodAnser; }
private WebMetodAnser GetWebResponse(WebMetodDescription webMetodDescription) { System.Net.ServicePointManager.Expect100Continue = false; WebMetodAnser webResponseAnser = new WebMetodAnser(); webResponseAnser.Error = null; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] postData = encoding.GetBytes(webMetodDescription.PostData); WebRequest request = WebRequest.Create(webMetodDescription.URI); request.Method = "POST"; request.ContentLength = postData.Length; request.ContentType = "application/x-www-form-urlencoded"; if (proxyUse){ WebProxy webProxy = new WebProxy(proxyServer, proxyPort); webProxy.Credentials = new NetworkCredential(proxyLogin, proxyPassword); request.Proxy = webProxy; } if (webMetodDescription.AUTHConnection == eAUTH.Secury){ request.Headers.Add("Key", key); HMACSHA512 hashMaker = new HMACSHA512(Encoding.ASCII.GetBytes(secret)); request.Headers.Add("Sign", ByteArrayToString(hashMaker.ComputeHash(postData)).ToLower()); } try{ Stream stream = request.GetRequestStream(); stream.Write(postData, 0, postData.Length); stream.Close(); WebResponse response = request.GetResponse(); webResponseAnser.Status = ((HttpWebResponse)response).StatusDescription; stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); webResponseAnser.StringReturn = reader.ReadToEnd(); return webResponseAnser; } catch (Exception ex){ webResponseAnser.Error = ex.ToString(); } return webResponseAnser; }