WebService—POST访问http接口
一、post请求
提交和返回数据都为json格式
统一采用UTF-8字符编码
1 public string Post(string Url, string jsonParas) 2 { 3 string strURL = Url; 4 //创建一个HTTP请求 5 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); 6 //Post请求方式 7 request.Method = "POST"; 8 //内容类型 9 request.ContentType = "application/x-www-form-urlencoded"; 10 11 //设置参数,并进行URL编码 12 13 string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas); 14 15 byte[] payload; 16 //将Json字符串转化为字节 17 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 18 //设置请求的ContentLength 19 request.ContentLength = payload.Length; 20 //发送请求,获得请求流 21 22 Stream writer; 23 try 24 { 25 writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象 26 } 27 catch (Exception) 28 { 29 writer = null; 30 Console.Write("连接服务器失败!"); 31 } 32 //将请求参数写入流 33 writer.Write(payload, 0, payload.Length); 34 writer.Close();//关闭请求流 35 // String strValue = "";//strValue为http响应所返回的字符流 36 HttpWebResponse response; 37 try 38 { 39 //获得响应流 40 response = (HttpWebResponse)request.GetResponse(); 41 } 42 catch (WebException ex) 43 { 44 response = ex.Response as HttpWebResponse; 45 } 46 Stream s = response.GetResponseStream(); 47 // Stream postData = Request.InputStream; 48 StreamReader sRead = new StreamReader(s); 49 string postContent = sRead.ReadToEnd(); 50 sRead.Close(); 51 return postContent;//返回Json数据 52 }
1 /// <summary> 2 /// POST数据到指定地址 3 /// </summary> 4 /// <param name="url"></param> 5 /// <param name="encoding"></param> 6 /// <param name="data"></param> 7 /// <returns></returns> 8 public static string PostDataToUrl(string url, System.Text.Encoding encoding, System.Collections.Specialized.NameValueCollection data) 9 { 10 //设置HttpWebRequest 11 System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(url)); 12 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; 13 request.ContentType = "application/x-www-form-urlencoded"; 14 request.Method = "POST"; 15 request.KeepAlive = false; 16 17 byte[] buffer; 18 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) 19 { 20 for (int i = 0; i < data.Count; i++) 21 { 22 buffer = encoding.GetBytes(data.Keys[i]); 23 if (i > 0) 24 ms.WriteByte(Convert.ToByte('&')); 25 ms.Write(buffer, 0, buffer.Length); 26 ms.WriteByte(Convert.ToByte('=')); 27 buffer = encoding.GetBytes(HttpUtility.UrlEncode(data[i])); 28 ms.Write(buffer, 0, buffer.Length); 29 } 30 buffer = ms.ToArray(); 31 } 32 33 request.ContentLength = buffer.Length; 34 using ( 35 36 37 System.IO.Stream requestStream = request.GetRequestStream()) 38 { 39 requestStream.Write(buffer, 0, buffer.Length); 40 requestStream.Close(); 41 } 42 43 try 44 { 45 //获取数据 46 using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)(request.GetResponse())) 47 using (System.IO.Stream stream = response.GetResponseStream()) 48 using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, encoding)) 49 { 50 string result = reader.ReadToEnd(); 51 reader.Close(); 52 stream.Close(); 53 response.Close(); 54 return result; 55 } 56 } 57 finally 58 { 59 request.Abort(); 60 } 61 } 62 63 调用方法: 64 Byte[] bytes = Encoding.UTF8.GetBytes(msg); 65 //转换成Base64字符串 66 msg = BytesToBase64(bytes); 67 string encodeMsgs = System.Web.HttpUtility.UrlEncode(msg);//urlencode编码 68 NameValueCollection vc = new NameValueCollection(); 69 vc.Add("sid", sid); 70 vc.Add("mobi", mobi); 71 vc.Add("sign", sign); 72 vc.Add("msg", encodeMsgs);
1 /// <summary> 2 /// post序列化访问接口 3 /// </summary> 4 /// <param name="url">接口路径</param> 5 /// <param name="order">参数</param> 6 /// <returns></returns> 7 public static string POST(string url, string order) 8 { 9 string WorkOrder = ""; 10 string strURL = url; 11 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); 12 13 request.ContentType = "application/json"; 14 request.Method = "POST"; 15 16 using (var streamWriter = new StreamWriter(request.GetRequestStream())) 17 { 18 string json = new JavaScriptSerializer().Serialize(new { mnum = order }); 19 streamWriter.Write(json); 20 } 21 HttpWebResponse response; 22 try 23 { 24 //获得响应流 25 response = (HttpWebResponse)request.GetResponse(); 26 } 27 catch (WebException ex) 28 { 29 response = ex.Response as HttpWebResponse; 30 31 } 32 Stream stream = response.GetResponseStream(); 33 34 //获取响应内容 35 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) 36 { 37 WorkOrder = reader.ReadToEnd(); 38 } 39 MES rb = JsonConvert.DeserializeObject<MES>(WorkOrder); 40 if (rb.result == 0) 41 { 42 rb.res = "false"; 43 return rb.res; 44 } 45 return rb.snnum; 46 47 }