Skip to content

Instantly share code, notes, and snippets.

@Briscoooe
Created January 24, 2017 12:12
Show Gist options
  • Save Briscoooe/b38e8835cf1e8ba9ea2333e2c75671a3 to your computer and use it in GitHub Desktop.
Save Briscoooe/b38e8835cf1e8ba9ea2333e2c75671a3 to your computer and use it in GitHub Desktop.
namespace Geocoding
{
class HttpRequestSender
{
public HttpResponseObject SendHttpRequest(string url, Stopwatch watch)
{
IPEndPoint remoteIp = null;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ServicePoint.BindIPEndPointDelegate = delegate(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
Console.WriteLine(remoteEndPoint);
remoteIp = remoteEndPoint;
return null;
};
watch.Start();
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
watch.Stop();
return new HttpResponseObject()
{
ResponseTime = watch.ElapsedMilliseconds,
IpAddress = remoteIp,
StatusCode = (int)response.StatusCode,
Headers = GetHeaderList(response),
ResponseBody = GetResponseBody(response)
};
}
private List<string> GetHeaderList(WebResponse response)
{
List<string> tempHeaderList = new List<string>();
for (int i = 0; i < response.Headers.Count; ++i)
{
tempHeaderList.Add(string.Format("{0} : {1}", response.Headers.Keys[i], response.Headers[i]));
}
return tempHeaderList;
}
private string GetResponseBody(WebResponse response)
{
var encoding = Encoding.ASCII;
using (var reader = new StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment