GET请求:
private JObject doGet(Dictionary<String, String> param)
{
JObject jres = new JObject();
object obj = new { notifyType = 1, taCode = strTANO, tradeDt = DateTime.Now.ToString("yyyyMMdd") };
strData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
string strURL = "https//192.168.1.1/channel/push";
if (!strURL.EndsWith("?"))
{
strURL = strURL + "?";
}
strURL = strURL + strData;
try
{
//构造一个Web请求的对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURL);
//获取web请求的响应的内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通过响应流构造一个StreamReader
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string response = reader.ReadToEnd();
reader.Close();
myResponse.Close();
jres.Add("code", new JValue("0"));
jres.Add("msg", new JValue(response));
}
catch (Exception ex)
{
log.Info("消息推送Get请求发生异常:" + ex.Message);
jres.Add("code", new JValue("9999"));
jres.Add("msg", new JValue(ex.Message));
}
return jres;
}
POST请求:
private JObject doPost(Dictionary<String, String> param)
{
object obj = new { notifyType = 1, taCode = strTANO, tradeDt = DateTime.Now.ToString("yyyyMMdd") };
strData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
string strURL = "https://192.168.1.1/post";
JObject jres = new JObject();
try
{
byte[] sendData = Encoding.UTF8.GetBytes(strData);
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/json");
byte[] retInfo = webClient.UploadData(strURL, "POST", sendData);
string strInfo = System.Text.Encoding.UTF8.GetString(retInfo);
JObject jre = Newtonsoft.Json.JsonConvert.DeserializeObject(strInfo) as Newtonsoft.Json.Linq.JObject;
string requestState = jre["code"] is null ? jre["result"] is null ? "9999" : jre["result"].ToString() : jre["code"].ToString();
if ("000000".Equals(requestState) || "success".Equals(requestState))
{
jres.Add("code", new JValue("0"));
}
else
{
jres.Add("code", new JValue("9999"));
}
}
catch (Exception ex)
{
log.Info("消息推送Post请求发生异常:" + ex.Message);
jres.Add("code", new JValue("9999"));
jres.Add("msg", new JValue(ex.Message));
}
return jres;
}