✅ C# noob needs help retrying WebClient .DownloadString
I am a C# noob, so bear that in mind. I am trying to write a downloads a website sourcecode using WebClient .DownloadString. 95% of the time, it succeeds. But on occassion, it fails, and crashes the program. I have tried just about anything I could think of to try and get it to redownload the site source code anything it fails... This is the source I have currently:
static string getPageSource(string url)
{
string pageSource = "";
WebClient client = new WebClient();
pageSource = client.DownloadString(url); if(pageSource.Length == 0) { pageSource = client.DownloadString(url); } return pageSource; }
pageSource = client.DownloadString(url); if(pageSource.Length == 0) { pageSource = client.DownloadString(url); } return pageSource; }
48 Replies
Don't use
WebClient
HttpClient
is what you should use
And downloading a string is as simple as
What header would I need to use, cause when I try to copy and paste the code you provided, it gives errors.
Depends what errors those are
No errors at all. The IDE is saying that the line of code is incorrect.
well you're using .net framework
you should be using .net 8 if possible
also your method is not async so await would not work and would display an error as well
$newproject
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework.
.NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended.
https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Oof, yeah, don't use .NET Framework
I am not gonna start the app all over in a different standard. I have an entire app written for downloading images from a gallery site I frequent. I am not gonna rewrite the entire thing from the ground up just to change the standerd. It all works. I just need to find out how to get it to redo WebClient..DownloadString if it fails. That is the only issue I have.
I mean httpclient exists on .net framework
Yeah, but not using statements
sure, it would be a bit different code but still better than webclient
So I guess
something like this?
+making the method async or using GetAwaiter().GetResult()
With all that said, there's plenty of software that already can download files from Rule34 and other boorus
Bionus/imgbrd-grabber is good
https://www.bionus.org/imgbrd-grabber/
Also, pretty sure most of them have APIs
So it will be easier to use that, rather than scraping the HTML page
@ZZZZZZZZZZZZZZZZZZZZZZZZZ Again, when I put your code in, the IDE gives me an error:
Well, you're trying to redeclare an existing variable
So that's error one
Then, you're trying to
await
inside of a non-async method
That's error twoOkat, how to i define the method itself as async?
The latter in particular will be hard to solve well with the version of .NET you're using, since there was no
async Main()
in the days of yore
static async Task<string> GetPageSource(string url)
Then, when you call this method, you will have to await
itNow I am getting errors on the method name itself:
Read what I wrote again
Carefully
Also, read the errors
Surprisingly, they tend to tell you what's wrong
Wild, I know
if i had to guess, it's mad because of syntax errors at the bottom of your file
but like z said, the errors will tell you what's wrong
switch to the "error list" tab at the bottom
oh, you just have a ridiculous amount of white space down there :KEKW:
Since you changed the name of the method I had written, I had to change its refernce in the code, but it is showing an error as well in the code:
you can call the method whatever you want
Angius
Then, when you call this method, you will have to
await
itQuoted by
<@85903769203642368> from #C# noob needs help retrying WebClient .DownloadString (click here)
React with ❌ to remove this embed.
I implore you, read what is being said
and yes, read the error
specifically the topmost one
being able to identify and look up errors to figure out how to fix them is an important part of programming
What does that error mean?
The method returns a
Task<string>
To get a string
out of it, it needs to be await
ed
I did not tell you that the call to this method needs to be awaited for shits and gigglesStill giving an error: '
read it
think about what it's saying
And what does the error tell you?
z, don't help him he needs to figure this out
this is a teaching a man to fish moment
What part of "C# NOOB NEEDS HELP
Needs help reading?
do you want to stay a C# noob?
we do expect a base level of effort and not just copying and pasting a solution and sending screenshots of errors back
Tried turning pageSource = GetPageSource(url); to pageSource = await GetPageSource(url); still getting an error.
yeah... so read the error
the error is telling you the problem, it's now up to you to act on it or ask for clarification if you don't understand what the error means
the solution is already in this thread
Fuck it..... I will just deal with the crashes if this is all the help I am gonna get.
this server is for helping people to learn, not to spoon feed solutions
if you don't want to learn there's not much we can do
if you'd like to try to solve the problem yourself and ask for more help than "fix my code for me" we'd be happy to
I wil just post it on stackoverflow then.... gotta be a billion times more helpful than this.
Oh boy :KEKW:
you get out what you put in
¯\_(ツ)_/¯
If you aren’t willing to read and try to understand error messages, you will never leave the noob stage.
If you don’t understand them, that’s fine, but you then need to ask specific questions.
My stackoverflow post WAS helpful. They had me add a try catch in my code, and I was able to figure out that the issues was that it was attempting requests too fast, and I put a pause in the code, and it is working without issue.
Good on you mate.
For new projects in the future, you should consider using net8.
WebClient has been deprecated for some time now.
You can close the thread using $close
If you have no further questions, please use /close to mark the forum thread as answered
also dont use a new httpclient for each request. you can keep doing requests with the same one which will work better