分享

C#通过发送 http 请求调用接口之Post

 昵称38509023 2017-01-19

 1.实现车辆审核回调接口(包含车辆基本信息数据)
(1)描述
本接口提供运政车辆基本信息回传服务,调用方式请使用 post 方式提交。
(2)接口调用示例:
待加密格式
type=xxx&user=xxx&pwd=x&vclN=xxx&pltClr=xx&vclClr=xx&area=xx&hsHld=xx...
(3)请求地址格式
http://xxx/vinfos?p=加密后数据

(4)实现: 

  1. void btnTransferVehicleAudit_Click(object sender, EventArgs e)  
  2. {  
  3.     DateTime dt = Convert.ToDateTime("2014-01-09 16:47:24");  
  4.     long dtNow = ConvertDateTimeToInt(DateTime.Now);  
  5.     long dtSpanTime = ConvertDateTimeToInt(dt);   
  6.   
  7.     StringBuilder sb = new StringBuilder();  
  8.     sb.Append("type=xxx&user=xxx&pwd=0&vclN=xxx&pltClr=2&vclClr=2&area=xx&hsHld=xx&vclCt=xx");  
  9.     sb.Append("&vclBsTp=xx&vclTps=xx&vclUsTp=xx&vclLng=xx&vclWdt=xx&vclHgt=xx&boxLng=xx&boxWdt=xx&boxHgt=xx");  
  10.     sb.Append("&boxCnt=xx&lftHgt=xx&twoTstStrd=xx&regLicN=xx&drvLicBgDt=xx&drvLicBrd=xx&vin=xx&vclOrg=xx&rdTrans=xx&opStt=xx"); ....  
  11.   
  12.     string strEn = Encrypt.Security.EnCode(sb.ToString());//加密  
  13.     string url = "http://114.242.194.231:8084/BasisInfoInterface/vinfos";  
  14.   
  15.     Stream outstream = null;  
  16.     Stream instream = null;  
  17.     StreamReader sr = null;  
  18.     HttpWebResponse response = null;  
  19.     HttpWebRequest request = null;  
  20.     Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");  
  21.   
  22.     byte[] data = encoding.GetBytes("p=" + strEn);  
  23.     // 准备请求...  
  24.     try  
  25.     {  
  26.         // 设置参数  
  27.         request = WebRequest.Create(url) as HttpWebRequest;  
  28.         CookieContainer cookieContainer = new CookieContainer();  
  29.         request.CookieContainer = cookieContainer;  
  30.         request.AllowAutoRedirect = true;  
  31.         request.Method = "POST";  
  32.         request.ContentType = "application/x-www-form-urlencoded";  
  33.         request.ContentLength = data.Length;  
  34.         outstream = request.GetRequestStream();  
  35.         outstream.Write(data, 0, data.Length);  
  36.         outstream.Close();  
  37.         //发送请求并获取相应回应数据  
  38.         response = request.GetResponse() as HttpWebResponse;  
  39.         //直到request.GetResponse()程序才开始向目标网页发送Post请求  
  40.         instream = response.GetResponseStream();  
  41.         sr = new StreamReader(instream, encoding);  
  42.         //返回结果网页(html)代码  
  43.         string content = sr.ReadToEnd();   
  44.         //处理返回值  
  45.         string quReply = Encrypt.Security.DeCode(content); //解密  
  46.         string jsona = @quReply;  
  47.         JavaScriptSerializer jscvt = new JavaScriptSerializer();  
  48.         //将jsona字符串转变成指定类型对象  
  49.         TransferResult objQuery = jscvt.Deserialize<TransferResult>(jsona);  
  50.   
  51.         switch (objQuery.code)  
  52.         {  
  53.             case "10000": ShowErrorMessage("添加成功2"); break;  
  54.             case "10001": ShowErrorMessage("添加失败"); return;  
  55.             case "20001": ShowErrorMessage("用户名或者密码错误"); return;  
  56.             case "30001": ShowErrorMessage("参数为空"); return;  
  57.             case "90001": ShowErrorMessage("其它异常"); return;  
  58.             case "90002": ShowErrorMessage("系统异常"); return;  
  59.         }  
  60.     }  
  61.     catch (Exception ex)  
  62.     {  
  63.         string err = ex.Message;   
  64.     }    
  65. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多