当前位置:网站首页>. Net code to implement get request and post request
. Net code to implement get request and post request
2022-06-13 07:03:00 【AndroidOrCSharp】
GET request :
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
{
// Construct a Web The object of the request
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURL);
// obtain web Content of the requested response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Construct a 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(" Message push Get Request exception :" + ex.Message);
jres.Add("code", new JValue("9999"));
jres.Add("msg", new JValue(ex.Message));
}
return jres;
}
POST request :
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(" Message push Post Request exception :" + ex.Message);
jres.Add("code", new JValue("9999"));
jres.Add("msg", new JValue(ex.Message));
}
return jres;
}
边栏推荐
- Detailed Mr Atlas of hip joint (Reprinted)
- How to make a development board from scratch? Illustrated and illustrated, step by step operation for you to see.
- JS method of extracting numbers from strings
- New Taishan crowdfunding business diversion fission growth model in 2022
- RT thread simulator lvgl control: switch switch button control
- 我的理财产品显示清算中是什么意思?
- 【RS-422与RS-485】RS-422与RS-485串行接口标准
- Differences between SQL and NoSQL of mongodb series
- Nfv basic overview
- Computer network interview questions
猜你喜欢
Normalizing y-axis in histograms in R ggplot to proportion
Comment utiliser le logiciel wangyou DFM pour l'analyse des plaques froides
What is the new business model of Taishan crowdfunding in 2022?
Tikv key performance parameters and optimization
学习Mysql基础第一天
牙周炎问题调研(持续进行中)
【马尔科夫链-蒙特卡罗】马尔科夫链-蒙特卡罗方法对先验分布进行抽样
【RS-422与RS-485】RS-422与RS-485串行接口标准
Learning notes of MySQL series by database and table
[cloud native | kubernetes] kubernetes configuration
随机推荐
Through the function seaborn cubehelix_ Palette build order palette
Smart finance is upgraded again, and jinglianwen technology provides data collection and labeling services
Normalizing y-axis in histograms in R ggplot to proportion
上位机开发(固件下载软件之编码调试)
105. 从前序与中序遍历序列构造二叉树
RT-Thread 模拟器 simulator LVGL控件:button 按钮事件
Lightning breakpoint continuation
Introduction and use of dumping
Tidb index optimization
Ansible PlayBook的中清单变量优先级分析及清单变量如何分离总结
1154. 一年中的第几天
Micro isolation (MSG)
Priority analysis of list variables in ansible playbook and how to separate and summarize list variables
That is, after the negative impact of gcat advertising e-commerce, is there no stable advertising e-commerce platform?
MySQL系列之分库分表学习笔记
杭州网上开户是安全的吗?
号称下一代监控系统 来看看它有多牛逼
Vsys of Isis (virtual system)
髋关节MR详细图谱(转载)
15、 IO stream (I)