////// 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; }
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, ListInsured_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; } }