博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
参数请求post, get , delete中的基本使用(1)
阅读量:5739 次
发布时间:2019-06-18

本文共 7125 字,大约阅读时间需要 23 分钟。

///         /// get 数据的时候可以采用此函数大部分功能        ///         ///         /// 
public string Starr_AuthoredGET(String[] args, CookieCollection UserCookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0] + args[1]);//将host和参数直接拼接 request.Method = "GET"; request.ContentType = args[2]; request.Accept = "*/*"; request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(UserCookie);//设置req的cookie HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); //UserCookie = response.Cookies;//设置最新的cookie reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } /// /// POST 数据的时候可以采用此函数大部分功能 /// /// ///
public string Starr_AuthoredPOST(String[] args, CookieCollection UserCookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]); request.Method = "POST"; request.ContentType = args[2]; request.Accept = "*/*"; request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(UserCookie);//设置req的cookie byte[] byteArray = Encoding.UTF8.GetBytes(args[1]);//输出流 request.ContentLength = byteArray.Length; Stream outStream = request.GetRequestStream(); outStream.Write(byteArray, 0, byteArray.Length); outStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); //UserCookie = response.Cookies;//设置最新的cookie reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } /// /// DELETE数据的时候可以采用此函数大部分功能 /// /// ///
public string Starr_AuthoredDelete(String[] args, CookieCollection UserCookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);//将host和参数直接拼接 request.Method = "DELETE"; request.ContentType = args[1]; request.Accept = "*/*"; request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(UserCookie);//设置req的cookie HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); //UserCookie = response.Cookies;//设置最新的cookie reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; }
View Code
public CookieCollection UserCookie = new CookieCollection();        ///         /// post 登录,将cookie保存到UserCookie,后续动作都要用到        ///         ///         /// 
public string Starr_login(out CookieCollection UserCookie) { String[] args = { "http://dicc2.ins24.com/ah/session", "{\"username\":\"" +Starr_username+ "\",\"password\":\"" + Starr_password + "\"}", "application/json" }; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]); request.Method = "POST"; request.ContentType = args[2]; request.Accept = "*/*"; request.CookieContainer = new CookieContainer(); byte[] byteArray = Encoding.UTF8.GetBytes(args[1]);//输出流 request.ContentLength = byteArray.Length; Stream outStream = request.GetRequestStream(); outStream.Write(byteArray, 0, byteArray.Length); outStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); UserCookie = response.Cookies;//设置req的cookie reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; }

 

方法调用如下:

 

public static Code.InsuranceModel.InsuranceSegment_Result Starr_GetApplayTeamRequestBook(InsuranceModel.InsuranceSegment_Policy Policy_model, List
Insured_Listmodel) { Code.Login Starr_model = new Code.Login(); Code.InsuranceModel.InsuranceSegment_Result result = new InsuranceModel.InsuranceSegment_Result(); try { string RequestJson = Code.InsuranceJson.Starr_RequestApplayTeamBookJson(Policy_model, Insured_Listmodel); string[] RequestArr = { "http://dicc2.ins24.com/ah/policies", RequestJson.ToString(), "application/json" }; //判断接口请求是否成功 result = Code.InsuranceDecodeJson.Starr_GetLoginDecodeJson(); if (result.Success == true) { //请求投保 string RequestBookJson = Starr_model.Starr_AuthoredPOST(RequestArr, result.UserCookie); //解析投保返回的json语句 Code.InsuranceModel.InsuranceSegment_PolicyBookMessage db_model = Code.InsuranceDecodeJson.Starr_GetApplayTeamBookDecodeJson(RequestBookJson,Policy_model.externalno); if (db_model != null) { if (db_model.status.Trim().Equals("SUCCESS")) { InsuranceCode.InsertInsuranceSegment_PolicyBookMessage(db_model); InsuranceCode.UpdateInsuranceSegment_Policy(db_model.downloadurl, db_model.npremium, db_model.policyno, db_model.policyid, db_model.status, db_model.service, db_model.successdate); result.Success =true; result.Message ="团保投保成功!"; } else { } } } else { } return result; } catch (Exception ex) { result.Success = false; result.Message = ex.Message; return result; } }
View Code

 

转载于:https://www.cnblogs.com/ly77461/p/5708255.html

你可能感兴趣的文章
centos和pycharm中取绝对路径的差别
查看>>
ext2磁盘布局
查看>>
MySql数据库2【常用命令行】
查看>>
动态规划---->货郎担问题
查看>>
添加虚拟子网
查看>>
Ubuntu 12.04 root用户登录设置
查看>>
存储过程点滴
查看>>
Maven编译跳过test的设置
查看>>
[LeetCode]22.Generate Parentheses
查看>>
计算A/B Test需要的样本量
查看>>
二叉树前序中序后序遍历的非递归方法
查看>>
mysql 行转列列转行
查看>>
《设计模式系列》---桥接模式
查看>>
[Unity3d]Shader 着色器 学习前了解知识
查看>>
Linux中文件颜色所代表的属性和颜色
查看>>
Redrain duilib中事件委托存在的问题
查看>>
43、我的C#学习笔记9
查看>>
字符串的简单操作
查看>>
C#新功能--命名参数与可选参数
查看>>
strtok和strtok_r
查看>>