求助:网络问题 HttpWebRequest 和 HttpWebResponse 为什么要定义 static?
请教:
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 后
程序会一直等待在这里
吧数据接收完
才会执行后面的程序
是吗?
谢谢!
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 后
程序会一直等待在这里
吧数据接收完
才会执行后面的程序
是吗?
谢谢!
...全文
相关推荐
浅谈C#中HttpWebRequest与HttpWebResponse的使用方法
**扩展应用**: - 使用HttpWebRequest和HttpWebResponse还可以实现文件上传下载、自定义HTTP头、超时控制等功能。
C#中HttpWebRequest、WebClient、HttpClient的使用详解
:```csharppublic static string HttpGet(string url, string postDataStr){ HttpWebRequest request = (HttpWebRequest
C#中HttpWebRequest介绍
下面是一个简单的GET请求实现:```csharppublic static HttpWebResponse CreateGetHttpResponse(string url, int?
HttpWebRequest和HttpWebResponse用法小结
本文主要介绍了如何使用HttpWebRequest和HttpWebResponse类进行HTTP通信,特别是在处理系统错误报告和异常捕获的场景下。在快速扩展市场的背景下,公司遇到的问题是,由于客
C#(Csharp)多线程HTTP并发请求(HttpWebRequest)采集蜘蛛
**HTTP请求与响应**:C#提供的HttpWebRequest和HttpWebResponse类提供了对HTTP协议的封装,使得我们能够方便地发起HTTP请求并接收响应。


