rusty rusty - 3 months ago 23
C# Question

Saving a HTTP get response to a file

I can not get the HTTP response to be saved to a text file. I can make a get request and I can see that it goes through and I get the response I want but it doesn't save to a file. My code is below, I have commented out the other solution I have tried.

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();

}


The Wireshark analysis of the TCP stream:

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>

Answer

Well, that would be something like below. I think you can adapt it just fine.

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);