函数

///<summary>///HttpRequest请求方式///</summary>///<paramname="webUrl">请求地址</param>///<paramname="contentType">请求Context-Type</param>///<paramname="dicPara">请求参数</param>///<paramname="method">请求方式</param>///<returns>返回请求结果</returns>publicstringRequestWeb(stringwebUrl,stringcontentType,Dictionary<string,string>dicPara=null,stringmethod="POST"){try{//创建一个请求项/**urlhttp://localhost:55563/WMSService.asmx/WMSPushService*url分为两段*第一段webServices发布地址eg:http://localhost:55563/WMSService.asmx*第二段将调用webServices的函数名eg:WMSPushService*/HttpWebRequestrequest=WebRequest.Create(webUrl)asHttpWebRequest;//判断服务器是否处理POST的数据request.ServicePoint.Expect100Continue=method.ToUpper().Equals("POST");//请求方式request.Method=method.ToUpper();//是否建立持久性链接request.KeepAlive=true;//设置HTTP头request.UserAgent="object.yan";//设置超时时间request.Timeout=Int32.MaxValue;//设置请求标题头request.ContentType=contentType;if(dicPara!=null){//参数经过URL编码/**webService请求函数中所包含的参数必须在paraUrlCoded中进行拼接*【且将参数与值进行URL字符串加密】*否则将返回服务器500错误*/StringBuildersbPara=newStringBuilder();/**循环加载参数信息*/foreach(varitemindicPara){if(!string.IsNullOrWhiteSpace(sbPara.ToString())){sbPara.Append("&");}sbPara.Append(System.Web.HttpUtility.UrlEncode(item.Key));sbPara.Append("="+System.Web.HttpUtility.UrlEncode(item.Value));}/**将字符串参数转为二进制数组*将其写入请求流中*/byte[]paraByte;//将URL编码后的字符串转化为字节paraByte=System.Text.Encoding.UTF8.GetBytes(sbPara.ToString());//设置请求的ContentLengthrequest.ContentLength=paraByte.Length;//获得请求流Streamwriter=request.GetRequestStream();//将请求参数写入流writer.Write(paraByte,0,paraByte.Length);//关闭请求流writer.Close();}/**获取webServices返回信息*/HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;//读取资源流信息StreamReadersr=newStreamReader(response.GetResponseStream(),Encoding.GetEncoding(response.CharacterSet));//获取html文本returnsr.ReadToEnd();}catch(Exceptionex){returnex.StackTrace+Environment.NewLine+ex.Message;}}


调用函数

Dictionary<string,string>dicPara=newDictionary<string,string>();dicPara.Add("logistics_interface",logistics_interface);dicPara.Add("data_digest",data_digest);dicPara.Add("warehouseCode",warehouseCode);RequestWeb("http://localhost:55563/WMSService.asmx/WMSPushService","application/x-www-form-urlencoded",dicPara)


调用WebServices时 Context-Type使用

application/x-www-form-urlencoded