您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 综合/其它 > 基于C的车辆违章查询接口调用代码实例
基于C#的车辆违章查询接口调用代码实例代码描述:基于C#的车辆违章查询接口调用代码实例相关平台:聚合数据usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingXfrog.Net;usingSystem.Diagnostics;usingSystem.Web;//----------------------------------//全国车辆违章调用示例代码-聚合数据//在线接口文档:代码中JsonObject类下载地址:{classProgram{staticvoidMain(string[]args){stringappkey=*******************;//配置您申请的appkey//1.获取支持城市参数接口stringurl1==newDictionarystring,string();parameters1.Add(province,);//默认全部,省份简写,如:ZJ、JSparameters1.Add(dtype,);//返回数据格式:json或xml或jsonp,默认jsonparameters1.Add(format,);//格式选择1或2,默认1parameters1.Add(callback,);//返回格式选择jsonp时,必须传递parameters1.Add(key,appkey);//你申请的keystringresult1=sendPost(url1,parameters1,get);JsonObjectnewObj1=newJsonObject(result1);StringerrorCode1=newObj1[error_code].Value;if(errorCode1==0){Debug.WriteLine(成功);Debug.WriteLine(newObj1);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj1[error_code].Value+:+newObj1[reason].Value);}//2.请求违章查询接口stringurl2==newDictionarystring,string();parameters2.Add(dtype,);//返回数据格式:json或xml或jsonp,默认jsonparameters2.Add(callback,);//返回格式选择jsonp时,必须传递parameters2.Add(key,appkey);//你申请的keyparameters2.Add(city,);//城市代码*parameters2.Add(hphm,);//号牌号码完整7位,需要utf8urlencode*parameters2.Add(hpzl,);//号牌类型,默认02parameters2.Add(engineno,);//发动机号(根据城市接口中的参数填写)parameters2.Add(classno,);//车架号(根据城市接口中的参数填写)stringresult2=sendPost(url2,parameters2,get);JsonObjectnewObj2=newJsonObject(result2);StringerrorCode2=newObj2[error_code].Value;if(errorCode2==0){Debug.WriteLine(成功);Debug.WriteLine(newObj2);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj2[error_code].Value+:+newObj2[reason].Value);}//3.接口剩余请求次数查询stringurl3==newDictionarystring,string();parameters3.Add(key,appkey);//你申请的keyparameters3.Add(dtype,);//返回数据的格式,xml或json,默认jsonstringresult3=sendPost(url3,parameters3,get);JsonObjectnewObj3=newJsonObject(result3);StringerrorCode3=newObj3[error_code].Value;if(errorCode3==0){Debug.WriteLine(成功);Debug.WriteLine(newObj3);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj3[error_code].Value+:+newObj3[reason].Value);}}///summary///Http(GET/POST)////summary///paramname=url请求URL/param///paramname=parameters请求参数/param///paramname=method请求方法/param///returns响应内容/returnsstaticstringsendPost(stringurl,IDictionarystring,stringparameters,stringmethod){if(method.ToLower()==post){HttpWebRequestreq=null;HttpWebResponsersp=null;System.IO.StreamreqStream=null;try{req=(HttpWebRequest)WebRequest.Create(url);req.Method=method;req.KeepAlive=false;req.ProtocolVersion=HttpVersion.Version10;req.Timeout=5000;req.ContentType=application/x-=utf-8;byte[]postData=Encoding.UTF8.GetBytes(BuildQuery(parameters,utf8));reqStream=req.GetRequestStream();reqStream.Write(postData,0,postData.Length);rsp=(HttpWebResponse)req.GetResponse();Encodingencoding=Encoding.GetEncoding(rsp.CharacterSet);returnGetResponseAsString(rsp,encoding);}catch(Exceptionex){returnex.Message;}finally{if(reqStream!=null)reqStream.Close();if(rsp!=null)rsp.Close();}}else{//创建请求HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(url+?+BuildQuery(parameters,utf8));//GET请求request.Method=GET;request.ReadWriteTimeout=5000;request.ContentType=text/html;charset=UTF-8;HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();StreammyResponseStream=response.GetResponseStream();StreamReadermyStreamReader=newStreamReader(myResponseStream,Encoding.GetEncoding(utf-8));//返回内容stringretString=myStreamReader.ReadToEnd();returnretString;}}///summary///组装普通文本请求参数。////summary///paramname=parametersKey-Value形式请求参数字典/param///returnsURL编码后的请求数据/returnsstaticstringBuildQuery(IDictionarystring,stringparameters,stringencode){StringBuilderpostData=newStringBuilder();boolhasParam=false;IEnumeratorKeyValuePairstring,stringdem=parameters.GetEnumerator();while(dem.MoveNext()){stringname=dem.Current.Key;stringvalue=dem.Current.Value;//忽略参数名或参数值为空的参数if(!string.IsNullOrEmpty(name))//&&!string.IsNullOrEmpty(value){if(hasParam){postData.Append(&);}postData.Append(name);postData.Append(=);if(encode==gb2312){postData.Append(HttpUtility.UrlEncode(value,Encoding.GetEncoding(gb2312)));}elseif(encode==utf8){postData.Append(HttpUtility.UrlEncode(value,Encoding.UTF8));}else{postData.Append(value);}hasParam=true;}}returnpostData.ToString();}///summary///把响应流转换为文本。////summary///paramname=rsp响应流对象/param///paramname=encoding编码方式/param///returns响应文本/returnsstaticstringGetResponseAsString(HttpWebResponsersp,Encodingencoding){System.IO.Streamstream=null;StreamReaderreader=null;try{//以字符流的方式读取HTTP响应stream=rsp.GetResponseStream();reader=newStreamReader(stream,encoding);returnreader.ReadToEnd();}finally{//释放资源if(reader!=null)reader.Close();if(stream!=null)
本文标题:基于C的车辆违章查询接口调用代码实例
链接地址:https://www.777doc.com/doc-2569042 .html