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

lsh9982010 lsh9982010 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请教:
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 后

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

是吗?

谢谢!
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
lsh9982010
C# 111,129 成员 642,538 内容