public void ReadResponseTextBackwardsCompatibilityTest() { const string testText = "<title>Lorem Ipsum - All the facts - Lipsum generator</title>"; var request = System.Net.WebRequest.Create("http://www.lipsum.com/"); var response = request.GetResponse(); var httpWebResponse = response as HttpWebResponse; Assert.NotNull(httpWebResponse); #pragma warning disable 618 var html = WebResponseFactory.ReadResponseText(httpWebResponse); #pragma warning restore 618 Assert.NotNull(html); Assert.Contains(testText, html); request = System.Net.WebRequest.Create("http://www.lipsum.com/"); response = request.GetResponse(); httpWebResponse = response as HttpWebResponse; Assert.NotNull(httpWebResponse); #pragma warning disable 618 html = WebResponseFactory.ReadResponseText(httpWebResponse, Encoding.GetEncoding("ISO-8859-1")); #pragma warning restore 618 Assert.NotNull(html); Assert.Contains(testText, html); }
/// <summary> /// Method for sending request to server /// </summary> /// <param name="username">User name to use to access system</param> /// <param name="password">Password for account to access system</param> /// <param name="url">API url to use</param> /// <param name="method">Put, Get, Delete, Post</param> /// <returns>String message</returns> public string SendRequest(string username, string password, string url, string method) { WebResponseFactory factory = new WebResponseFactory(); try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"))); request.ContentType = ParameterContentTypeSettings.applicationjson; request.ContentLength = 0; Task <WebResponse> response = Task.Factory.FromAsync(request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), null); ///wait to get a response from the server do { Thread.Sleep(GeneralSettings.WebRequestSleep); } while (response.IsCompleted == false); //We have a response, now process it. if (response.IsCompleted) { return(factory.ProcessResponse((HttpWebResponse)response.Result)); } return(factory.ProcessResponse(null)); } catch { return(factory.ProcessResponse(null)); } }
public void GetEncodingBackwardsCompatibilityTest() { var request = System.Net.WebRequest.Create("https://www.wikipedia.org/"); var response = request.GetResponse(); var httpWebResponse = response as HttpWebResponse; Assert.NotNull(httpWebResponse); #pragma warning disable 618 Assert.Equal(Encoding.GetEncoding("ISO-8859-1"), WebResponseFactory.GetEncoding(httpWebResponse)); #pragma warning restore 618 }
/// <summary> /// Method for sending request to server with a content body /// </summary> /// <param name="username">User name to use to access system</param> /// <param name="password">Password for account to access system</param> /// <param name="url">API url to use</param> /// <param name="method">Put, Get, Delete, Post</param> /// <param name="contentBody">JSON data to send in the body of the request.</param> /// <returns>String message</returns> public string SendRequest(string username, string password, string url, string method, string contentBody) { WebResponseFactory factory = new WebResponseFactory(); try { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] encodedData = encoding.GetBytes(contentBody); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"))); request.ContentType = ParameterContentTypeSettings.applicationjson; request.ContentLength = encodedData.Length; var requestStream = request.GetRequestStream(); requestStream.Write(encodedData, 0, encodedData.Length); requestStream.Close(); Task <WebResponse> response = Task.Factory.FromAsync(request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), null); ///wait to get a response from the server do { Thread.Sleep(GeneralSettings.WebRequestSleep); } while (response.IsCompleted == false); //We have a response, now process it. if (response.IsCompleted) { return(factory.ProcessResponse((HttpWebResponse)response.Result)); } return(factory.ProcessResponse(null)); } catch { return(factory.ProcessResponse(null)); } }