public static bool SendHttpWebRequest <T>(string url, string httpRequestMtd, string strParam, JSONReturnFormat rtFormat, ref T record, ref string strResult) { bool bSuccess = false; try { Trace.WriteLine(String.Format("HttpRequest {0} {1}", url, strParam)); // byte[] byteArray = Encoding.UTF8.GetBytes(strParam); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); IWebProxy webProxy = WebRequest.DefaultWebProxy; if (null != webProxy) { webProxy.Credentials = CredentialCache.DefaultNetworkCredentials; request.Proxy = webProxy; } request.CookieContainer = m_CookieContainer; request.Method = httpRequestMtd; request.ContentType = "application/json; charset=UTF-8"; request.Credentials = CredentialCache.DefaultCredentials; // request.ContentLength = byteArray.Length; StreamWriter newStream = new StreamWriter(request.GetRequestStream(), Encoding.UTF8); newStream.Write(strParam); newStream.Flush(); newStream.Close(); WebResponse response = (WebResponse)request.GetResponse(); StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string text2 = sr2.ReadToEnd(); Trace.WriteLine(String.Format("HttpResponse {0} {1}", url, text2)); strResult = text2; if (ParseJsonData <T>(text2, rtFormat, ref record)) { bSuccess = true; } response.Close(); } catch (Exception ex) { Trace.WriteLine(ex.Message); bSuccess = false; } return(bSuccess); }
public static bool ParseJsonData <T>(string strSrc, JSONReturnFormat rtFormat, ref T record) { bool bSuccess = false; try { JObject o = JObject.Parse(strSrc); if (null != strSrc && null != record) { string strValue; switch (rtFormat) { case JSONReturnFormat.JSONResultFormat1: strValue = o.GetValue("response").ToString(); ((JSONResultFormat1)(dynamic)record).m_nResponse = Convert.ToInt32(strValue); strValue = o.GetValue("message").ToString(); ((JSONResultFormat1)(dynamic)record).m_strMessage = strValue; strValue = o.GetValue("success").ToString(); if ("true" == strValue || "True" == strValue) { ((JSONResultFormat1)(dynamic)record).m_bSuccess = true; } else { ((JSONResultFormat1)(dynamic)record).m_bSuccess = false; } bSuccess = true; break; case JSONReturnFormat.JSONResultFormat2: strValue = o.GetValue("message").ToString(); ((JSONResultFormat2)(dynamic)record).m_strMessage = strValue; strValue = o.GetValue("success").ToString(); if ("true" == strValue || "True" == strValue) { ((JSONResultFormat2)(dynamic)record).m_bSuccess = true; } else { ((JSONResultFormat2)(dynamic)record).m_bSuccess = false; } bSuccess = true; break; case JSONReturnFormat.SessionInfo: strValue = o.GetValue("totalCount").ToString(); ((SessionInfo)(dynamic)record).m_nTotalCount = Convert.ToInt32(strValue); strValue = o.GetValue("detail").ToString(); ((SessionInfo)(dynamic)record).m_strDetail = strValue; strValue = o.GetValue("code").ToString(); ((SessionInfo)(dynamic)record).m_nErrorCode = Convert.ToInt32(strValue); strValue = o.GetValue("success").ToString(); if ("true" == strValue || "True" == strValue) { ((SessionInfo)(dynamic)record).m_bSuccess = true; } else { ((SessionInfo)(dynamic)record).m_bSuccess = false; } strValue = o.GetValue("addon").ToString(); { JObject jSub = JObject.Parse(strValue); string strItemValue = jSub.GetValue("merchant_id").ToString().Trim(new char[] { '\r', '\n', ' ' }); ((SessionInfo)(dynamic)record).m_nMerchantID = Convert.ToInt32(strItemValue); strItemValue = jSub.GetValue("customer").ToString().Trim(new char[] { '\r', '\n', ' ' }); jSub = JObject.Parse(strItemValue); strItemValue = jSub.GetValue("customer_id").ToString().Trim(new char[] { '\r', '\n', ' ' }); ((SessionInfo)(dynamic)record).m_nCustomerID = Convert.ToInt32(strItemValue); } bSuccess = true; break; default: break; } } } catch { } return(bSuccess); }