1: /// <summary>
2: /// Makes a request to a Uniform Resource Identifier (URI) for accessing data from the Internet.
3: /// </summary>
4: /// <param name="requestUriString">The URI that identifies the Internet resource.</param>
5: /// <param name="proxyAddress">The URI of the proxy server.</param>
6: /// <returns>Returns a response to an Internet request.</returns>
7: /// <exception cref="System.Exception">Returns a message that describes the current exception.</exception>
8: /// <example>
9: /// GetWebResponse("http://twitter.com/DeveloperInfra", null);
10: /// </example>
11: private static System.String GetWebResponse(System.String requestUriString, System.String proxyAddress)
12: {
13: System.Net.WebResponse webResponse = null;
14: System.IO.StreamReader stream = null;
15: System.String result = null;
16:
17: try
18: {
19: /* Initialize the web request. */
20: System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(requestUriString);
21: /* Optionally specify the User Agent. */
22: webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
23: /* Optionally create a proxy for the request object to use if you sit behind a firewall. */
24: if (System.String.IsNullOrEmpty(proxyAddress) == false)
25: {
26: System.Net.WebProxy webProxy = new System.Net.WebProxy(proxyAddress);
27: webRequest.Proxy = webProxy;
28: }
29:
30: /* Make a synchronous request and convert the response into something we can consume. */
31: webResponse = webRequest.GetResponse();
32: stream = new System.IO.StreamReader(webResponse.GetResponseStream());
33: result = stream.ReadToEnd();
34: }
35: catch (Exception ex)
36: {
37: //TODO: Log and handle the exception rather than just pass it back.
38: result = System.String.Format("Error: {0}", ex.ToString());
39: }
40: finally
41: {
42: /* Clean-up system resources. */
43: if (stream != null) { stream.Close(); }
44: if (webResponse != null) { webResponse.Close(); }
45: }
46:
47: return result;
48: }