예제 #1
0
        public static HttpResult Get(string address, string username, string password)
        {
            var httpResult = new HttpResult();
            try
            {
                var result = "";

                using (var client = new WebClient())
                {
                    client.Headers.Add("Authorization",
                        "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)));
                    using (var reader = new StreamReader(client.OpenRead(address)))
                    {
                        var s = reader.ReadToEnd();
                        result = s;
                    }
                }
                httpResult.Html = result;
                httpResult.StatusCode = HttpStatusCode.OK;
                httpResult.Error = "";
                return httpResult;
            }
            catch (Exception e)
            {
                httpResult.Html = "";
                if (e.GetType().Name == "WebException")
                {
                    var we = (WebException)e;
                    var response = (HttpWebResponse)we.Response;
                    httpResult.StatusCode = response.StatusCode;
                    httpResult.Error = we.Message;
                }
                else
                {
                    httpResult.StatusCode = HttpStatusCode.InternalServerError;
                    httpResult.Error = e.Message;
                }

                return httpResult;
            }
        }
예제 #2
0
        public static HttpResult Get(string address, string baseUrl)
        {
            var httpResult = new HttpResult();
            try
            {
                var result = "";

                using (var client = new WebClient())
                {
                    using (var reader = new StreamReader(client.OpenRead(address)))
                    {
                        var s = reader.ReadToEnd();
                        result = s;
                    }
                }
                httpResult.Html = result;
                httpResult.StatusCode = HttpStatusCode.OK;
                httpResult.Error = "";
                return httpResult;
            }
            catch (Exception e)
            {
                httpResult.Html = "";
                if (e.GetType().Name == "WebException")
                {
                    var we = (WebException)e;
                    var response = (HttpWebResponse)we.Response;
                    httpResult.StatusCode = response.StatusCode;
                    httpResult.Error = we.Message;
                }
                else
                {
                    httpResult.StatusCode = HttpStatusCode.InternalServerError;
                    httpResult.Error = e.Message;
                }

                return httpResult;
            }
        }