温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# Request请求——可用于WebService

发布时间:2020-07-23 20:14:06 来源:网络 阅读:1116 作者:objecttozero 栏目:编程语言

函数

/// <summary> /// HttpRequest 请求方式 /// </summary> /// <param name="webUrl">请求地址</param> /// <param name="contentType">请求Context-Type</param> /// <param name="dicPara">请求参数</param> /// <param name="method">请求方式</param> /// <returns>返回请求结果</returns> public string RequestWeb(string webUrl, string contentType, Dictionary<string, string> dicPara = null, string method = "POST") {     try     {         //创建一个请求项         /*             * url http://localhost:55563/WMSService.asmx/WMSPushService             * url 分为两段             * 第一段 webServices 发布地址        eg:http://localhost:55563/WMSService.asmx             * 第二段 将调用webServices的函数名   eg:WMSPushService             */         HttpWebRequest request = WebRequest.Create(webUrl) as HttpWebRequest;         //判断服务器是否处理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 错误                 */             StringBuilder sbPara = new StringBuilder();             /*                 * 循环加载参数信息                 */             foreach (var item in dicPara)             {                 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());             //设置请求的ContentLength             request.ContentLength = paraByte.Length;             //获得请求流             Stream writer = request.GetRequestStream();             //将请求参数写入流             writer.Write(paraByte, 0, paraByte.Length);             //关闭请求流             writer.Close();         }         /*          * 获取webServices返回信息          */         HttpWebResponse response = request.GetResponse() as HttpWebResponse;         //读取资源流信息         StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));         //获取html文本         return sr.ReadToEnd();     }     catch (Exception ex)     {         return ex.StackTrace + Environment.NewLine + ex.Message;     } }


调用函数

Dictionary<string, string> dicPara = new Dictionary<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



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI