String strURL = "Post请求地址"; System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); request.Method = "POST"; HttpWebRequest.DefaultWebProxy = null; request.ContentType = "application/json;charset=utf-8"; string data = "{\"key\":\"value\"}"; byte[] param = System.Text.Encoding.UTF8.GetBytes(data); request.ContentLength = param.Length; System.IO.Stream writer = request.GetRequestStream(); writer.Write(param, 0, param.Length); writer.Close(); System.Net.HttpWebResponse response; try { response = (System.Net.HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (System.Net.HttpWebResponse)ex.Response; } System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); //返回的结果 string strResult = myreader.ReadToEnd(); myreader.Close();
通过base64加密:
string str = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(strJson));