求助:网络问题 HttpWebRequest 和 HttpWebResponse 为什么要定义 static?

lsh9982010 2020-07-20 11:43:27
请教:
private void button1_Click(object sender, EventArgs e)
{ System.Text.StringBuilder sb = new StringBuilder();
sb.Append("{");

sb.Append("}");
string ret = Utils.WebUtils.postUrl("http://192.168.0.4:8080/emp/2", sb.ToString());
label1.Text = "服务器结果返回:" + ret;
}


public static string postUrl(string url,string body)
{
try
{

var data = body;

string strURL = url;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = data;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();


System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();

string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}

return strValue;
}
catch(Exception ex)
{
return "";
}

}


问题1:
public static string postUrl(string url,string body)

为什么要定义 static?

问题2:
sb.Append("{");
sb.Append("}");

这个 {} 有什么用?

问题3:
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}

当下 button1 后

程序会一直等待在这里
吧数据接收完
才会执行后面的程序

是吗?

谢谢!
...全文
4831 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
比如说你的代码
 label1.Text = "服务器结果返回:" +  ret;
这里编译器识别 this,也就是说实际代码是
 this.label1.Text = "服务器结果返回:" +  ret;


面向对象,主要就是体现在这个 this 上,处理代码知道自己处理对象是谁,不会认错自己的家人。而一个 static 代码不需要认识 this,不需要在运行时识别对象实例的具体类型来调用不同的子类 override 的方法,是编译时硬编码的简单的函数/方法。
  • 打赏
  • 举报
回复
在 .net 中,实际上类型也是一种对象。假设在 A 类代码中写一个 static 方法,那么这个方法肯定是用到本类型对象就够了,而不需要用到类型实例对象。
江湖评谈 2020-07-20
  • 打赏
  • 举报
回复
static 表示你这个方法属于类,一种设计方式加两个大 括号实质上是一json形式返回,参照json格式,他主要是 判断下一行不为null,否则就一直停止
lsh9982010 2020-07-20
  • 打赏
  • 举报
回复
Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); 这 3 句程序有什么作用? 分别是什么意思? 谢谢!
  • 打赏
  • 举报
回复
引用 5 楼 lsh9982010 的回复:
谢谢大家 1 但是我 吧 static 去掉,编译就出错 如果我不用 static 那要怎么改? 2: treamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } 当下 button1 后 程序会一直等待在这里 吧数据接收完 才会执行后面的程序 是吗? 谢谢!
不是静态方法你就得实例化才能访问 string ret =new Utils.WebUtils().postUrl("http://192.168.0.4:8080/emp/2", sb.ToString()); 是的会等待
lsh9982010 2020-07-20
  • 打赏
  • 举报
回复
谢谢大家 1 但是我 吧 static 去掉,编译就出错 如果我不用 static 那要怎么改? 2: treamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } 当下 button1 后 程序会一直等待在这里 吧数据接收完 才会执行后面的程序 是吗? 谢谢!
threenewbee 2020-07-20
  • 打赏
  • 举报
回复
因为不需要访问类的状态,这样的函数(相当于全局函数)就定义成static

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧