当前位置:网站首页>. 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;
}
边栏推荐
- Lightning data import
- Brief introduction to basic usage of echart
- 【騰訊阿裏最全面試題集錦】(四面:3輪技術+1輪HR)
- 上位机开发(固件下载软件之架构设计)
- Ansible PlayBook的中清单变量优先级分析及清单变量如何分离总结
- 【微弱瞬态信号检测】混沌背景下微弱瞬态信号的SVM检测方法的matlab仿真
- Upper computer development (detailed design of firmware download software)
- 髋关节MR详细图谱(转载)
- Recently, the popular social e-commerce marketing model, blind box e-commerce, how beautiful every second is accurately drained
- Host computer development (Architecture Design of firmware download software)
猜你喜欢

Detailed Mr Atlas of hip joint (Reprinted)

Yolov5 analysis | parameters and performance indicators

2022-06-12:在N*N的正方形棋盘中,有N*N个棋子,那么每个格子正好可以拥有一个棋子。 但是现在有些棋子聚集到一个格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二维数组代表,一

牙周炎问题调研(持续进行中)

10 Honest Facts I Want To Share With All Junior Developers

WWDC2022最大的亮点: MetalFX

What is the essence of social e-commerce disruption? How can businesses get more traffic?

Application of DS18B20 temperature sensor based on FPGA

Tidb dashboard modify port

RT-Thread 模拟器 simulator LVGL控件:button 按钮事件
随机推荐
IIS batch bind domain name
汇编语言基础:寄存器和寻址方式
Brief introduction to basic usage of echart
[Collection des questions d'examen les plus complètes de Tencent Ali] (quatre côtés: 3 tours de technologie + 1 tour de RH)
检测循环数“142857“
Xiaomi's one-sided interview questions (self sorting answers)
In the era of membership based social e-commerce, how do businesses build their own private domain traffic pool?
【騰訊阿裏最全面試題集錦】(四面:3輪技術+1輪HR)
2022-06-12:在N*N的正方形棋盘中,有N*N个棋子,那么每个格子正好可以拥有一个棋子。 但是现在有些棋子聚集到一个格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二维数组代表,一
牙周炎问题调研(持续进行中)
What is online blind box e-commerce? Can blind box e-commerce become a new bonus market for the Internet?
[Tencent Alibaba's most comprehensive collection of test questions] (four sides: three rounds of technology +1 round of HR)
It's called the next generation monitoring system. Let's see how awesome it is
Priority analysis of list variables in ansible playbook and how to separate and summarize list variables
面试必刷算法TOP101之单调栈 TOP31
号称下一代监控系统 来看看它有多牛逼
10 Honest Facts I Want To Share With All Junior Developers
Try to use renderdoc to view the shader code of UE
How worker threads in the thread pool are recycled
What does my financial product mean in clearing?