码迷,mamicode.com
首页 > Web开发 > 详细

.net post请求webservice

时间:2019-09-18 11:19:04      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:header   sts   stream   innertext   uri   ebs   show   encoding   line   

技术图片
class Program
    {
        static void Main(string[] args)
        {
            var m = "m=" + JsonConvert.SerializeObject(new { age = 1, name = "jxp" });
            var a = HttpHelper.PostRequest("http://localhost:7132/Jxp.asmx/HelloWorld", DataTypeEnum.Form, m);
            var xmldoc = new XmlDocument();
            xmldoc.LoadXml(a);
            var nsMgr = new XmlNamespaceManager(xmldoc.NameTable); nsMgr.AddNamespace("ns", "http://tempuri.org/");
            XmlNode errorNode = xmldoc.SelectSingleNode("/ns:string", nsMgr);
            var b = JsonConvert.DeserializeObject(errorNode.InnerText);
            Console.WriteLine(b);
            Console.ReadKey();
        }
    }
控制台调用
技术图片
[WebMethod]
public string HelloWorld(string m)
{
      return m;
}
webservice服务
技术图片
public class HttpHelper
    {
        /// <summary>
        /// Post带参请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="contentType">指定参数类型</param>
        /// <param name="strData"></param>
        /// <param name="dicHeader"></param>
        /// <returns></returns>
        public static string PostRequest(string url, DataTypeEnum contentType, string strData, Dictionary<string, string> dicHeader = null)
        {
            string result;
            var webRequest = WebRequest.Create(url);
            if (dicHeader != null)
                foreach (var m in dicHeader)
                {
                    webRequest.Headers.Add(m.Key, m.Value);
                }
            webRequest.Method = MethodTypeEnum.Post.ToString();
            webRequest.Proxy = null;
            if (contentType == DataTypeEnum.Form)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";
            }
            else
            {
                webRequest.ContentType = "application/" + contentType;
            }
            
            byte[] reqBodyBytes = System.Text.Encoding.UTF8.GetBytes(strData);
            Stream reqStream = webRequest.GetRequestStream();//加入需要发送的参数
            reqStream.Write(reqBodyBytes, 0, reqBodyBytes.Length);
            reqStream.Close();
            using (var reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        /// <summary>
        /// Get不带参请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetRequest(string url)
        {
            string result;
            WebRequest webRequest = WebRequest.Create(url);
            webRequest.Method = MethodTypeEnum.Get.ToString();
            webRequest.Proxy = null;
            using (var reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
    }

    /// <summary>
    /// 带参数据类型
    /// </summary>
    public enum DataTypeEnum
    {
        Json,
        Xml,
        Form
    }

    /// <summary>
    /// 带参数据类型
    /// </summary>
    public enum MethodTypeEnum
    {
        Get,
        Post
    }
请求帮助类

 返回json

技术图片

 .net post请求webservice

.net post请求webservice

标签:header   sts   stream   innertext   uri   ebs   show   encoding   line   

原文地址:https://www.cnblogs.com/jxp0202/p/11540055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!