zoukankan      html  css  js  c++  java
  • C#请求http post和get

    首先先要感谢博主小伟地方提供的博客,让我解决了问题。

    同样是先提问题,我们要请求http干什么?

    通过请求http,传入我的参数,我希望能够获取到项目里面的某些数据,比如这里,我们需要得到SceneList。

    1.参数,传入catagoryid可以得到在这个分类下的场景列表

    2.在控制台,把场景列表信息打印出来。

    首先GET方法

    static void Main(string[] args)
    {
        //GET方法
                //首先创建一个httpRequest,用Webrequest.Create(url)的方法
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://localhost:5534/Home/SceneList?catagoryid=2");
                //初始化httpRequest,申明用的GET方法请求http
                httpRequest.Timeout = 2000;
                httpRequest.Method = "GET";
                //创建一个httpResponse,存放服务器返回信息
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                //这个地方我也不晓得干了啥,反正都是抄写别人的
                //就理解为读取页面吧
                StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                //页面读完了,result接收结果
                string result = sr.ReadToEnd();
                //一个新字符串,其中当前实例中出现的所有指定字符串都替换为另一个指定的字符串 Replace(string oldValue,string newValue)
                result = result.Replace("
    ", "").Replace("
    ", "").Replace("	", "");
                //请求状态的反馈
                int status = (int)httpResponse.StatusCode;
                sr.Close();
    
                Console.WriteLine(result);
                Console.ReadKey();
    }
    GET

    然后POST方法

     static void Main(string[] args)
    {
        //POST方法
                //实例化一个编码方法
                UTF8Encoding encoding = new UTF8Encoding();
                //传入参数
                string postData = "catagoryid=2";
                //将参数编码为UTF8的格式
                byte[] data = encoding.GetBytes(postData);
    
                HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create("http://localhost:5534/Home/SceneList");
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string result = reader.ReadToEnd();
                result = result.Replace("
    ", "").Replace("
    ", "").Replace("	", "");
                int status = (int)myResponse.StatusCode;
                reader.Close();
    
                Console.WriteLine(result);
                Console.ReadKey();
    }
    POST

    其实POST和GET很像,就是传参数的方式不同,GET方法是在url后面传入参数 http://localhost:5534/Home/SceneList?catagoryid=2

    而POST方法,先申明参数,然后转码,之后在写入的时候带上参数 newStream.Write(data, 0, data.Length);

    这样也解决了目前的问题,再次感谢博主小伟地方提供的博客。

    注:此篇随笔只供参考使用,而且也有很多小瑕疵,最主要的不是代码,逻辑才是最重要的。

  • 相关阅读:
    【mysql】mac上基于tar.gz包安装mysql服务
    【maven】在idea上创建maven多模块项目
    关于Class.getResource和ClassLoader.getResource的路径问题
    【maven】Maven打包后为何文件大小改变了
    git常用命令
    第一章 第一个spring boot程序
    第二章 eclipse中m2e插件问题
    第一章 mac下开发环境的配置
    第一章 开发中遇到的错误列表
    第十一章 企业项目开发--消息队列activemq
  • 原文地址:https://www.cnblogs.com/Joker37/p/7246635.html
Copyright © 2011-2022 走看看