聚合数据常用快递接口代码

聚合数据常用快递接口
申请地址:https://www.juhe.cn/docs/api/id/43

  1. ashx代码

 publicvoid ProcessRequest(HttpContext context)

       {

           context.Response.ContentType = "text/plain";

           string appkey ="";//配置您申请的appkey                         

  //1.常用快递查询API           

           string url1 ="http://v.juhe.cn/exp/index";

           var parameters1 =newDictionary();

           parameters1.Add("com","");//需要查询的快递公司编号           

           parameters1.Add("no","");//需要查询的订单号           

           parameters1.Add("key", appkey);//你申请的key           

           parameters1.Add("dtype","json");//返回数据的格式,xml或json,默认json            

           string result1 = sendPost(url1,parameters1,"get");

           JsonObject newObj1 =newJsonObject(result1);

           String errorCode1 =newObj1["error_code"].Value;

           if (errorCode1 =="0")

           {

              context.Response.Write(newObj1);

               return;

           }

           else

           {

               Console.WriteLine(newObj1["error_code"].Value + ":" + newObj1["reason"].Value);

           }

       }

 
       publicbool IsReusable

       {

           get

           {

               returnfalse;

           }

       }

       ///

       /// Http (GET/POST)

       ///

       ///请求URL

       ///请求参数

       ///请求方法

       ///响应内容

       staticstring sendPost(string url, IDictionary parameters,string method)

       {

           if (method.ToLower()=="post")

           {

               HttpWebRequest req =null;

               HttpWebResponse rsp =null;

               System.IO.Stream reqStream =null;

               try

               {

                   req = (HttpWebRequest)WebRequest.Create(url);

                   req.Method = method;

                   req.KeepAlive = false;

                   req.ProtocolVersion = HttpVersion.Version10;

                   req.Timeout = 5000;

                   req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

                   byte[] postData =Encoding.UTF8.GetBytes(BuildQuery(parameters,"utf8"));

                   reqStream =req.GetRequestStream();

                   reqStream.Write(postData,0, postData.Length);

                   rsp = (HttpWebResponse)req.GetResponse();

                   Encoding encoding =Encoding.GetEncoding(rsp.CharacterSet);

                   returnGetResponseAsString(rsp, encoding);

               }

               catch (Exception ex)

               {

                   return ex.Message;

               }

               finally

               {

                   if (reqStream !=null) reqStream.Close();

                   if (rsp !=null) rsp.Close();

               }

           }

           else

           {

               //创建请求

               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" +BuildQuery(parameters,"utf8"));

 

               //GET请求

               request.Method = "GET";

               request.ReadWriteTimeout =5000;

               request.ContentType = "text/html;charset=UTF-8";

               HttpWebResponse response = (HttpWebResponse)request.GetResponse();

               Stream myResponseStream =response.GetResponseStream();

               StreamReader myStreamReader =newStreamReader(myResponseStream,Encoding.GetEncoding("utf-8"));

 

               //返回内容

               string retString =myStreamReader.ReadToEnd();

               return retString;

           }

       }

 

       ///

       ///组装普通文本请求参数。

       ///

       ///Key-Value形式请求参数字典

       ///URL编码后的请求数据

       staticstring BuildQuery(IDictionary parameters,string encode)

       {

           StringBuilder postData =newStringBuilder();

           bool hasParam =false;

           IEnumerator> dem =parameters.GetEnumerator();

           while (dem.MoveNext())

           {

               string name =dem.Current.Key;

               string value =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();

       }

 

       ///

       ///把响应流转换为文本。

       ///

       ///响应流对象

       ///编码方式

       ///响应文本

       staticstring GetResponseAsString(HttpWebResponse rsp, Encoding encoding)

       {

           System.IO.Stream stream =null;

           StreamReader reader =null;

           try

           {

               //以字符流的方式读取HTTP响应

               stream =rsp.GetResponseStream();

               reader = newStreamReader(stream, encoding);

               return reader.ReadToEnd();

           }

           finally

           {

               //释放资源

               if (reader !=null) reader.Close();

               if (stream !=null) stream.Close();

               if (rsp !=null) rsp.Close();

           }

       }
  

 2.前台代码

 3.常见问题

XMLHttpRequest.status=200 (正常响应)
XMLHttpRequest.readyState=4 (正常接收)
ajax也会提示一个parseerror的错误.

可能的原因是返回的数据格式与你ajax中写的数据格式不一致,返回的是text格式,而你写的是json格式

JQuery将文本转化成JSON对象需要注意的问题

在JQuery的许多方法中,很多方法的参数可以传入一个JSON对象,比如Ajax方法的第二个参数。怎么将文本转化成JSON对象,需要注意以下问题

 varcpro_id="u2261530";(window["cproStyleApi"] =window["cproStyleApi"] ||{})[cpro_id]={at:"3",rsi0:"680",rsi1:"200",pat:"6",tn:"baiduCustNativeAD",rss1:"#FFFFFF",conBW:"1",adp:"1",ptt:"0",titFF:"%E5%BE%AE%E8%BD%AF%E9%9B%85%E9%BB%91",titFS:"14",rss2:"#000000",titSU:"0",ptbg:"90",piw:"0",pih:"0",ptp:"0"}

1)$.parseJSON方法返回的是一个字符串,而不是JSON对象。
2)要将字符串转化成对象,很容易想起JS中的eval方法。事实上是可以的,不过需要加上括号。如var js="{\"PageIndex\":\"1\"}";varobj=eval("("+js+")");。不过使用eval,是不安全的,因为其可以编译任何js代码。
3)下载一个JSON解析器,因为其只认可JSON文本。这样就比较安全了。
JSON官方网站提供了这么一个脚本。地址:http://www.JSON.org/json2.js。使用起来比较简单,引入该文件后,如:JSON.parse($("#ctl00_ContentPlaceHolder1_hfSearch").val().toString())。
4)使用JSON.parse方法或者是$.parseJSON方法时,注意JSON数据的name和value用双引号括起来,对于$.parseJSON方法,还要将JSON字符串用单引号括起来再转换,对于JSON.parse方法,就不必了。真TMD的浪费时间。 注意这几个技巧,在操作JSON数据时,可以少走不少弯路。
————————————————
版权声明:本文为CSDN博主「beautifulsarah」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/beautif...

你可能感兴趣的:(api)