Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webclient The remote server returned an error: (500) Internal Server Error

I'm trying to download a webpage using webclient and get the 500 internal error

public class AsyncWebClient
    {
        public string GetContent(string url)
        {
            return GetWebContent(url).Result.ToString();
        }
        private Task<string> GetWebContent(string url)
        {
            var wc = new WebClient();
            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();

            wc.DownloadStringCompleted += (obj, args) =>
            {
                if (args.Cancelled == true)
                {
                    tcs.TrySetCanceled();
                    return;
                }

                if (!String.IsNullOrEmpty(args.Result))
                    tcs.TrySetResult(args.Result);
            };

            wc.DownloadStringAsync(new Uri(url));
            return tcs.Task;
        }
    }

and call:

var wc =new AsyncWebClient();
var html = wc.GetContent("http://truyen.vnsharing.net/");    >> always get the above error

if i use other site, then it works just fine. don't know what's special in this site.

Please help !!

like image 415
nam vo Avatar asked Feb 17 '13 09:02

nam vo


People also ask

How do I fix 500 Internal server error in Linux?

Start clearing your browser cache. Unless the page that displays an error of 500 is cached, the browser should request a version of the page after clearing the cache. Then, come back. Until then, the web developer could fix the server problem.

What does server returned 500 mean?

The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request. This error response is a generic "catch-all" response.


Video Answer


1 Answers

The server is most likely expecting a correct User-Agent.

Update your code to the following:

var wc = new WebClient();
wc.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
like image 194
Jensen Avatar answered Sep 21 '22 06:09

Jensen