1 // <copyright file="HttpHelper.cs" company="Microsoft Open Technologies, Inc."> 2 // Copyright 2011-2013 Microsoft Open Technologies, Inc. All rights reserved. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // </copyright> 16 17 using System; 18 using System.Collections.Generic; 19 using System.Diagnostics; 20 using System.Diagnostics.CodeAnalysis; 21 using System.IO; 22 using System.Net; 23 using System.Text; 24 using System.Threading.Tasks; 25 26 namespace JabbR.Client 27 { 28 internal static class HttpHelper 29 { 30 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are flowed back to the caller.")] GetHttpResponseAsync(this HttpWebRequest request)31 public static Task<HttpWebResponse> GetHttpResponseAsync(this HttpWebRequest request) 32 { 33 try 34 { 35 return Task.Factory.FromAsync<HttpWebResponse>(request.BeginGetResponse, ar => (HttpWebResponse)request.EndGetResponse(ar), null); 36 } 37 catch (Exception ex) 38 { 39 return TaskAsyncHelper.FromError<HttpWebResponse>(ex); 40 } 41 } 42 43 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are flowed back to the caller.")] GetHttpRequestStreamAsync(this HttpWebRequest request)44 public static Task<Stream> GetHttpRequestStreamAsync(this HttpWebRequest request) 45 { 46 try 47 { 48 return Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null); 49 } 50 catch (Exception ex) 51 { 52 return TaskAsyncHelper.FromError<Stream>(ex); 53 } 54 } 55 GetAsync(string url, Action<HttpWebRequest> requestPreparer)56 public static Task<HttpWebResponse> GetAsync(string url, Action<HttpWebRequest> requestPreparer) 57 { 58 HttpWebRequest request = CreateWebRequest(url); 59 if (requestPreparer != null) 60 { 61 requestPreparer(request); 62 } 63 return request.GetHttpResponseAsync(); 64 } 65 66 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Callers check for null return.")] ReadAsString(this HttpWebResponse response)67 public static string ReadAsString(this HttpWebResponse response) 68 { 69 try 70 { 71 using (response) 72 { 73 using (Stream stream = response.GetResponseStream()) 74 { 75 var reader = new StreamReader(stream); 76 77 return reader.ReadToEnd(); 78 } 79 } 80 } 81 catch (Exception ex) 82 { 83 #if NET35 84 Debug.WriteLine(String.Format(System.Globalization.CultureInfo.InvariantCulture, "Failed to read response: {0}", ex)); 85 #else 86 Debug.WriteLine("Failed to read response: {0}", ex); 87 #endif 88 // Swallow exceptions when reading the response stream and just try again. 89 return null; 90 } 91 } 92 CreateWebRequest(string url)93 private static HttpWebRequest CreateWebRequest(string url) 94 { 95 HttpWebRequest request = null; 96 #if WINDOWS_PHONE 97 request = (HttpWebRequest)WebRequest.Create(url); 98 request.AllowReadStreamBuffering = false; 99 #elif SILVERLIGHT 100 request = (HttpWebRequest)System.Net.Browser.WebRequestCreator.ClientHttp.Create(new Uri(url)); 101 request.AllowReadStreamBuffering = false; 102 #else 103 request = (HttpWebRequest)WebRequest.Create(url); 104 #endif 105 return request; 106 } 107 } 108 } 109