private static T request <T>(WebClient agent, string url, string postData) { string response = ""; T resObj; try { agent.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] rawResponse = agent.UploadData(url, "POST", Encoding.UTF8.GetBytes(postData)); response = Encoding.UTF8.GetString(rawResponse); resObj = fastJSON.JSON.Instance.ToObject <T>(response); } catch (WebException e) { throw new WammerCloudException("Wammer cloud error", postData, e); } catch (Exception e) { throw new WammerCloudException("Wammer cloud error", postData, response, e); } if (resObj is CloudResponse) { CloudResponse cres = resObj as CloudResponse; if (cres.status != 200 || cres.api_ret_code != 0) { throw new WammerCloudException("Wammer cloud error", postData, response, cres.api_ret_code); } } return(resObj); }
private static T request <T>(WebClient agent, string url, string parameters, bool isGet) { string response = ""; T resObj; try { agent.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] rawResponse = null; if (isGet) { if (string.IsNullOrEmpty(parameters)) { rawResponse = agent.DownloadData(url); } else { rawResponse = agent.DownloadData(url + "?" + parameters); } } else { rawResponse = agent.UploadData(url, "POST", Encoding.UTF8.GetBytes(parameters)); } response = Encoding.UTF8.GetString(rawResponse); resObj = fastJSON.JSON.Instance.ToObject <T>(response); } catch (WebException e) { throw new WammerCloudException("Wammer cloud error", parameters, e); } catch (Exception e) { throw new WammerCloudException("Wammer cloud error", parameters, response, e); } if (resObj is CloudResponse) { CloudResponse cres = resObj as CloudResponse; if (cres.status != 200 || cres.api_ret_code != 0) { throw new WammerCloudException("Wammer cloud error", parameters, response, cres.api_ret_code); } } return(resObj); }
private static int TryParseWammerError(string resText) { if (resText == null) { return(-1); } try { CloudResponse res = fastJSON.JSON.Instance.ToObject <CloudResponse>(resText); return(res.api_ret_code); } catch { return(-1); } }