• 【C#】Http发送Get&Post请求


    代码如下:

    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;
            }     
    

      结果如下:

  • 相关阅读:
    ES6~Promise的原理及使用二 理解Promise规范(copy自:https://www.cnblogs.com/lvdabao/p/5320705.html?utm_source=tuicool&utm_medium=referral)
    Google的Python代码格式化工具YAPF详解
    在ubuntu上使用QQ的经历
    Ubuntu 14.04 下安装Skype
    pip install lxml mysql-python error
    情人节的宠物-测试小工具
    戴维营收集
    工作日志(10.29)
    求职面试
    面试题集(转载)
  • 原文地址:https://www.cnblogs.com/Med1tator/p/6382379.html
Copyright © 2020-2023  润新知