public void CanParse_Url() { Url url = new Url("http://localhost/my/path/to/the/file.aspx?query=nothing#fragment"); Assert.AreEqual("http", url.Scheme); Assert.AreEqual("localhost", url.Authority); Assert.AreEqual("/my/path/to/the/file.aspx", url.Path); Assert.AreEqual("query=nothing", url.Querystring); Assert.AreEqual("fragment", url.Fragment); Assert.AreEqual(".aspx", url.Extension); Assert.AreEqual("/my/path/to/the/file.aspx?query=nothing", url.PathAndQuery); }
/// <summary> /// Posts the request. /// </summary> /// <param name="url">The URL.</param> /// <param name="userAgent">The user agent.</param> /// <param name="timeout">The timeout.</param> /// <param name="formParameters">The form parameters.</param> /// <param name="proxy">The proxy.</param> /// <returns></returns> public virtual string PostRequest(Url url, string userAgent, int timeout, string formParameters, IWebProxy proxy) { ServicePointManager.Expect100Continue = false; var request = (HttpWebRequest)WebRequest.Create(url); if (proxy != null) request.Proxy = proxy; request.UserAgent = userAgent; request.Timeout = timeout; request.Method = "POST"; request.ContentLength = formParameters.Length; request.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; request.KeepAlive = true; using (var myWriter = new StreamWriter(request.GetRequestStream())) { myWriter.Write(formParameters); } var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode < HttpStatusCode.OK && response.StatusCode >= HttpStatusCode.Ambiguous) { throw new InvalidResponseException( string.Format(CultureInfo.InvariantCulture, "The service was not able to handle our request. Http Status '{0}'.", response.StatusCode), response.StatusCode); } string responseText; using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) { // They only return "true" or "false". responseText = reader.ReadToEnd(); } return responseText; }
/// <summary> /// Posts the request and returns a text response. /// This is all that is needed for Akismet. /// </summary> /// <param name="url">The URL.</param> /// <param name="userAgent">The user agent.</param> /// <param name="timeout">The timeout.</param> /// <param name="formParameters">The properly formatted parameters.</param> /// <returns></returns> public virtual string PostRequest(Url url, string userAgent, int timeout, string formParameters) { return PostRequest(url, userAgent, timeout, formParameters, null); }