步骤
1. 编辑并Encoding提交的数据,格式类似 表单域名称1=值1&表单域名称2=值2&表单域名称3=值3……;
2. 发送请求(HttpWebRequest);
3. 获得回复(HttpWebResponse)。
- private string PostRequest(string url, string[] paramName, string[] paramValue)
- {
- // 编辑并Encoding提交的数据
- StringBuilder sbuilder = new StringBuilder(paramName[0] + "=" + paramValue[0]);
- for (int i = 1; i < paramName.Length; i++)
- sbuilder.Append("&" + paramName[i] + "=" + paramValue[i]);
- byte[] data = new ASCIIEncoding().GetBytes(sbuilder.ToString());
- // 发送请求
- System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = data.Length;
- Stream stream = request.GetRequestStream();
- stream.Write(data, 0, data.Length);
- stream.Close();
- // 获得回复
- System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string result = reader.ReadToEnd();
- reader.Close();
- return result;
- }