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

来源:互联网 发布:海报模板网站 知乎 编辑:程序博客网 时间:2024/04/28 16:58

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)实现: 

        void btnTransferVehicleAudit_Click(object sender, EventArgs e)        {            DateTime dt = Convert.ToDateTime("2014-01-09 16:47:24");            long dtNow = ConvertDateTimeToInt(DateTime.Now);            long dtSpanTime = ConvertDateTimeToInt(dt);             StringBuilder sb = new StringBuilder();            sb.Append("type=xxx&user=xxx&pwd=0&vclN=xxx&pltClr=2&vclClr=2&area=xx&hsHld=xx&vclCt=xx");            sb.Append("&vclBsTp=xx&vclTps=xx&vclUsTp=xx&vclLng=xx&vclWdt=xx&vclHgt=xx&boxLng=xx&boxWdt=xx&boxHgt=xx");            sb.Append("&boxCnt=xx&lftHgt=xx&twoTstStrd=xx&regLicN=xx&drvLicBgDt=xx&drvLicBrd=xx&vin=xx&vclOrg=xx&rdTrans=xx&opStt=xx"); ....             string strEn = Encrypt.Security.EnCode(sb.ToString());//加密            string url = "http://114.242.194.231:8084/BasisInfoInterface/vinfos";            Stream outstream = null;            Stream instream = null;            StreamReader sr = null;            HttpWebResponse response = null;            HttpWebRequest request = null;            Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");            byte[] data = encoding.GetBytes("p=" + strEn);            // 准备请求...            try            {                // 设置参数                request = WebRequest.Create(url) as HttpWebRequest;                CookieContainer cookieContainer = new CookieContainer();                request.CookieContainer = cookieContainer;                request.AllowAutoRedirect = true;                request.Method = "POST";                request.ContentType = "application/x-www-form-urlencoded";                request.ContentLength = data.Length;                outstream = request.GetRequestStream();                outstream.Write(data, 0, data.Length);                outstream.Close();                //发送请求并获取相应回应数据                response = request.GetResponse() as HttpWebResponse;                //直到request.GetResponse()程序才开始向目标网页发送Post请求                instream = response.GetResponseStream();                sr = new StreamReader(instream, encoding);                //返回结果网页(html)代码                string content = sr.ReadToEnd();                 //处理返回值                string quReply = Encrypt.Security.DeCode(content); //解密                string jsona = @quReply;                JavaScriptSerializer jscvt = new JavaScriptSerializer();                //将jsona字符串转变成指定类型对象                TransferResult objQuery = jscvt.Deserialize<TransferResult>(jsona);                switch (objQuery.code)                {                    case "10000": ShowErrorMessage("添加成功2"); break;                    case "10001": ShowErrorMessage("添加失败"); return;                    case "20001": ShowErrorMessage("用户名或者密码错误"); return;                    case "30001": ShowErrorMessage("参数为空"); return;                    case "90001": ShowErrorMessage("其它异常"); return;                    case "90002": ShowErrorMessage("系统异常"); return;                }            }            catch (Exception ex)            {                string err = ex.Message;             }          }


0 0
原创粉丝点击