using System; using System.Text; using System.IO; using System.Net; using System.Web; namespace APIStore { class HttpRequest { /* * HTTP的Post请求方式(推荐) * strUrl 请求地址 * param 请求数据 */ public string requestPost(string strUrl, string param) { HttpWebRequest httpWebRequest = WebRequest.Create(strUrl) as HttpWebRequest; httpWebRequest.Method = "POST"; //指定允许数据发送的请求的一个协议方法 httpWebRequest.ContentType = "application/x-www-form-urlencoded"; //设置 ContentType 属性设置为适当的值 byte[] data = Encoding.UTF8.GetBytes(param); using (Stream stream = httpWebRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); //写入数据 } WebResponse webResponse = httpWebRequest.GetResponse() as HttpWebResponse; //发起请求,得到返回对象 Stream dataStream = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.UTF8); string returnStr = reader.ReadToEnd(); // Clean up the streams and the response. reader.Close(); webResponse.Close(); return returnStr; } /** * 获取内容 */ public void getContent() { string url="https://v.1dq.com/api/c43" string param="key=您申请的key&bankcard=123456789&apiversion=2.0.5" string returnStr = null; returnStr = "post result:" + this.requestPost(url, param); Console.WriteLine(returnStr); } } }