.net post请求webservice

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

全文结束