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

同样是先提问题,我们要请求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 = ;
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("\r", "").Replace("\n", "").Replace("\t", "");
//请求状态的反馈
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, , data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "");
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);

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

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

最新文章

  1. Color国际青年公寓
  2. Linux for QQ 安装
  3. SharePoint 2013 对象模型操作"网站设置"菜单
  4. Android中TextView中的文字设置为不同颜色
  5. Who Gets the Most Candies?(线段树 + 反素数 )
  6. BZOJ3028 食物 (生成函数)
  7. php 解压 .gz 文件
  8. 对JavaScript对象数组按指定属性和排序方向进行排序
  9. postMan 使用
  10. Nhibernate 智能提示 以及其他类库智能提示
  11. qt model/view 架构自定义模型之QStringListModel
  12. javascript的事件冒泡,阻止事件冒泡和事件委托, 事件委托是事件冒泡的一个应用。
  13. java类集框架(ArrayList,LinkedList,Vector区别)
  14. vxWorks内核实现基本原理
  15. mysql创建新用户出现错误处理
  16. 如何在mac下安装php
  17. vagrant 同时设置多个同步目录
  18. HTML中Meta标签中http-equiv属性
  19. SSM项目问题中遇到 GET请求中有中文的情况
  20. ElasticSearch-.net平台下c#操作ElasticSearch详解

热门文章

  1. constexpr函数------c++ primer
  2. Struts2学习第一天--Struts2的概述、Struts2的入门、Struts2常见的配置、Struts2的Action的编写
  3. poi将图片导入excel(Java代码)
  4. 1232: 买不到的数目 [DP、数学]
  5. Kylin -- Dup key found 问题
  6. 《Andrew Ng深度学习》笔记5
  7. HDU-2063(二分图匹配模板题)
  8. R语言学习笔记(四)
  9. ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接
  10. WebApi接入Swagger