HttpWebRequest的问题,大家看看!
HttpWebRequest发送以后.在函数里messagebox.show出来是空的,调用函数出来也是空的.写入log.txt里却是有的.这是为什么,怎么才能让函数返回有数据!
public string sendTxt(string url)
{
HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(url);
try
{
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
rlog("log.txt", sr.ReadToEnd());//写入log.txt里有值的.
MessageBox.Show(sr.ReadToEnd());//这里是空的
return sr.ReadToEnd().ToString();
}
catch (System.Exception e)
{
rlog("log.txt", e.ToString());
return e.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
string myresult = sendTxt("http://www.baidu.com");
textBox1.Text = myresult;//这里也是空的
MessageBox.Show(myresult);//这里是空的
}
--------------------编程问答-------------------- 定义个string变量接收就行,
ReadToEnd是一次性从头读到尾。
string s=sr.ReadToEnd());--------------------编程问答--------------------
rlog("log.txt", s);
MessageBox.Show(s);
return s;
string s=sr.ReadToEnd();
rlog("log.txt", s);
MessageBox.Show(s);
return s;
补充:.NET技术 , C#