/// <summary> /// Perform an Http GET or POST operation, passing cookies, post data, /// user name and password, and return the result. /// </summary> /// <example> /// <code> /// using Argentini.Halide; /// ... /// string retVal = H3Http.GetWebPage("http://www.site.com", H3Http.GetWebPageMethod.Get, cookies, postData, "jsmith", "password"); /// </code> /// </example> /// <param name="url">Fully qualified URL to the remote page.</param> /// <param name="method">GetWebPageMethod enum value.</param> /// <param name="cookies">CookieContainer variable for sending cookies.</param> /// <param name="postData">String post data for sending a post.</param> /// <param name="userName">User name string for Windows Authentication.</param> /// <param name="password">password string for Windows Authentication.</param> /// <param name="timeoutMilliseconds">Timeout in milliseconds for the request.</param> /// <returns>A string with the result from the GET or POST operation.</returns> public static String GetWebPage(String url, GetWebPageMethod method, CookieContainer cookies, String postData, String userName, String password, Int32 timeoutMilliseconds) { String response = ""; try { HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); objRequest.ReadWriteTimeout = timeoutMilliseconds; objRequest.Timeout = timeoutMilliseconds; if (!String.IsNullOrEmpty(userName) || !String.IsNullOrEmpty(password)) { NetworkCredential nc = new NetworkCredential(); nc.UserName = userName; nc.Password = password; objRequest.Credentials = nc; } objRequest.AllowWriteStreamBuffering = true; objRequest.Method = method.ToString().ToUpper(); if (cookies != null) objRequest.CookieContainer = cookies; objRequest.AllowAutoRedirect = true; objRequest.Timeout = 30000; objRequest.Proxy = null; objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"; if (method == GetWebPageMethod.Post && !String.IsNullOrEmpty(postData)) { objRequest.ContentType = "application/x-www-form-urlencoded"; using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream())) myWriter.Write(postData); } HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); //if redirected, this is the final uri //FinalURL = objResponse.ResponseUri.ToString(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) response = sr.ReadToEnd(); } catch (Exception err) { throw new Exception("Halide.H3Http Error: " + err.ToString()); } return response; }
/// <summary> /// Perform an Http GET or POST operation, passing cookies, post data, /// user name and password, and return the result. /// </summary> /// <example> /// <code> /// using Argentini.Halide; /// ... /// string retVal = H3Http.GetWebPage("http://www.site.com", H3Http.GetWebPageMethod.Get, cookies, postData, "jsmith", "password"); /// </code> /// </example> /// <param name="url">Fully qualified URL to the remote page.</param> /// <param name="method">GetWebPageMethod enum value.</param> /// <param name="cookies">CookieContainer variable for sending cookies.</param> /// <param name="postData">String post data for sending a post.</param> /// <param name="userName">User name string for Windows Authentication.</param> /// <param name="password">password string for Windows Authentication.</param> /// <returns>A string with the result from the GET or POST operation.</returns> public static String GetWebPage(String url, GetWebPageMethod method, CookieContainer cookies, String postData, String userName, String password) { return GetWebPage(url, method, cookies, postData, userName, password, 300000); }
/// <summary> /// Perform an Http GET or POST operation, and return the result. /// </summary> /// <example> /// <code> /// using Argentini.Halide; /// ... /// string retVal = H3Http.GetWebPage("http://www.site.com", Halide.H3Http.GetWebPageMethod.Get, postData); /// </code> /// </example> /// <param name="url">Fully qualified URL to the remote page.</param> /// <param name="method">GetWebPageMethod enum value.</param> /// <param name="postData">String post data for sending a post.</param> /// <returns>A string with the result from the GET or POST operation.</returns> public static String GetWebPage(String url, GetWebPageMethod method, String postData) { return GetWebPage(url, method, null, postData, null, null); }