我无法将HTTP响应保存到文本文件中。我可以发出一个获取请求,我可以看到它通过,我得到了我想要的响应,但它没有保存到文件。我的代码如下,我已经注释了我尝试过的其他解决方案。
protected void Button1_Click(object sender, EventArgs e)
{
string input = TextBox1.Text;
string sURL;
sURL = "http://api.urlvoid.com/api1000/key/host/" + input + "/";
HttpWebRequest wrGETURL = (HttpWebRequest)WebRequest.Create(sURL);
HttpWebResponse wrResponse = (HttpWebResponse)wrGETURL.GetResponse();
StreamWriter SW = new StreamWriter(@"C:\Users\user\Desktop\API_Call_Test.txt");
Stream DataStream = wrResponse.GetResponseStream();
StreamReader SR = new StreamReader(DataStream);
string AllResponse = SR.ReadToEnd();
//var file = File.Open(@"C:\Users\user\Desktop\API_Call_Test.txt", FileMode.Create);
SW.Write(DataStream);
//DataStream.CopyTo(file);
wrResponse.Close();
}
TCP流的Wireshark分析:
GET /api1000/key/host/mo.gov/ HTTP/1.1
Host: api.urlvoid.com
Connection: Keep-Alive
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 12 Aug 2016 16:31:15 GMT
Content-Type: text/xml
Content-Length: 909
Age: 0
<?xml version="1.0" encoding="UTF-8"?>
<response>
<details>
<host>host</host>
<updated>1463838832</updated>
<http_response_code>200</http_response_code>
<domain_age>0</domain_age>
<google_page_rank>0</google_page_rank>
<alexa_rank>0</alexa_rank>
<connect_time>0.005995</connect_time>
<header_size>959</header_size>
<download_size>54726</download_size>
<speed_download>67090</speed_download>
<external_url_redirect></external_url_redirect>
<ip>
<addr>104.16.20.39</addr>
<hostname></hostname>
<asn>13335</asn>
<asname>CloudFlare, Inc.</asname>
<country_code>US</country_code>
<country_name>United States</country_name>
<region_name>California</region_name>
<city_name>San Francisco</city_name>
<continent_code>NA</continent_code>
<continent_name>North America</continent_name>
<latitude>37.7697</latitude>
<longitude>-122.393</longitude>
</ip>
</details>
<page_load>0.00</page_load>
</response>
答案 0 :(得分:3)
嗯,那就像下面这样。我认为你可以很好地适应它。
string url = "http://www.google.com";
string file = "response.txt";
string content = string.Empty;
// set up request/response
var request = (HttpWebRequest) WebRequest.Create(url);
var response = (HttpWebResponse) request.GetResponse();
var stream = response.GetResponseStream();
// read response content
using (var reader = new StreamReader(stream ?? new MemoryStream(), Encoding.UTF8))
content = reader.ReadToEnd();
// write to file on desktop
File.WriteAllText(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), file),
content,
Encoding.UTF8);
答案 1 :(得分:1)
您没有编写请求数据,而您需要一个流阅读器来读取数据:
protected void Button1_Click(object sender, EventArgs e)
{
string input = TextBox1.Text;
string sURL;
sURL = "http://api.urlvoid.com/api1000/key/host/" + input + "/";
HttpWebRequest wrGETURL = (HttpWebRequest)WebRequest.Create(sURL);
using (Stream stm = wrGETURL.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(input);
}
}
using (StreamReader responseReader = new StreamReader(wrGETURL.GetResponse().GetResponseStream()))
{
string resultString = responseReader.ReadToEnd();
if (File.Exists(@"C:\Users\user\Desktop\API_Call_Test.txt"))
File.Delete(@"C:\Users\user\Desktop\API_Call_Test.txt");
File.Create(@"C:\Users\user\Desktop\API_Call_Test.txt").Close();
File.WriteAllText(@"C:\Users\user\Desktop\API_Call_Test.txt", resultString);
}
}