C#通过发送 http 请求调用接口之Post
2014-07-31 11:40
681 查看
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)描述
本接口提供运政车辆基本信息回传服务,调用方式请使用 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®LicN=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; } }
相关文章推荐
- C#通过发送 http 请求调用接口之Get
- JAVA发送http get/post请求,调用http接口、方法
- [导入]C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#发送HttpPost请求调用WebService
- python通过http请求发送soap报文进行webservice接口调用
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- JAVA发送http get/post请求、调用http接口、方法
- C#发送HttpPost请求来调用WebService的方法
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- C#中发送HttpPost请求来调用WebService
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- JAVA发送http get/post请求,调用http接口、方法
- C#中发送HttpPost请求来调用WebService
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- JAVA发送http get/post请求,调用http接口、方法