代码如下:
1.控制台测试端代码
class Program { static void Main(string[] args) { #region Get请求测试 //string url = "http://localhost:42023/LoginHandler.aspx"; //string data = "UserName=admin&Password=123"; //string result = HttpGet(url, data); //Console.WriteLine(result); #endregion #region Post请求测试 string url = "http://localhost:42023/LoginHandler.aspx"; string data = "UserName=admin&Password=123"; string result = HttpPost(url, data); Console.WriteLine(result); #endregion Console.ReadLine(); } /// <summary> /// Http发送Get请求方法 /// </summary> /// <param name="Url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public static string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } /// <summary> /// Http发送Post请求方法 /// </summary> /// <param name="Url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public static string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postDataStr.Length; StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII); writer.Write(postDataStr); writer.Flush(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; //默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); string retString = reader.ReadToEnd(); return retString; } }
2.aspx页面代码(LoginHandler.aspx)
protected void Page_Load(object sender, EventArgs e) { #region Get请求 //string result = ""; //string userName = Request.QueryString["UserName"]; //string password = Request.QueryString["Password"]; //if (userName == "admin" && password == "123") //{ // result = "登陆成功"; //} //else //{ // result = "登陆失败"; //} //Response.Write(result); #endregion #region Post请求 string result = ""; string userName = Request.Form["UserName"]; string password = Request.Form["Password"]; if (userName == "admin" && password == "123") { result = "登陆成功"; } else { result = "登陆失败"; } Response.Write(result); #endregion }
3. 附加:
**获取Http图片网址的图片
代码如下:
private void button1_Click(object sender, EventArgs e) { HttpWebResponse resp; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://images2015.cnblogs.com/blog/1086526/201701/1086526-20170113145445213-1527663261.png"); //设置req.GetResponse()和req.GetRequestStream()的超时值 req.Timeout = 150000; resp = (HttpWebResponse)req.GetResponse(); System.Drawing.Image img; img = new System.Drawing.Bitmap(resp.GetResponseStream()); pictureBox1.BackgroundImage = img; } /// <summary> /// 从Http上图片地址获取图片 /// </summary> /// <param name="imgUrl"></param> /// <returns></returns> public Image GetHttpImg(string imgUrl) { Image result; HttpWebRequest req=(HttpWebRequest)HttpWebRequest.Create(imgUrl); HttpWebResponse resp =(HttpWebResponse) req.GetResponse(); result = new Bitmap(resp.GetResponseStream()); return result; }
结果如下: