标签: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; }
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
标签:header sts stream innertext uri ebs show encoding line
原文地址:https://www.cnblogs.com/jxp0202/p/11540055.html