C
C#5mo ago
YoItsTrev

✅ 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; }
48 Replies
Angius
Angius5mo ago
Don't use WebClient HttpClient is what you should use And downloading a string is as simple as
using var client = new HttpClient();
var str = await client.GetStringAsync(url);
using var client = new HttpClient();
var str = await client.GetStringAsync(url);
YoItsTrev
YoItsTrevOP5mo ago
What header would I need to use, cause when I try to copy and paste the code you provided, it gives errors.
Angius
Angius5mo ago
Depends what errors those are
YoItsTrev
YoItsTrevOP5mo ago
No errors at all. The IDE is saying that the line of code is incorrect.
No description
leowest
leowest5mo ago
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
MODiX
MODiX5mo ago
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
Angius
Angius5mo ago
Oof, yeah, don't use .NET Framework
YoItsTrev
YoItsTrevOP5mo ago
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.
leowest
leowest5mo ago
I mean httpclient exists on .net framework
Angius
Angius5mo ago
Yeah, but not using statements
leowest
leowest5mo ago
sure, it would be a bit different code but still better than webclient
Angius
Angius5mo ago
So I guess
using (var client = new HttpClient())
{
var str = await client.GetStringAsync(url);
}
using (var client = new HttpClient())
{
var str = await client.GetStringAsync(url);
}
something like this?
leowest
leowest5mo ago
+making the method async or using GetAwaiter().GetResult()
Angius
Angius5mo ago
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
YoItsTrev
YoItsTrevOP5mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ Again, when I put your code in, the IDE gives me an error:
No description
Angius
Angius5mo ago
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 two
YoItsTrev
YoItsTrevOP5mo ago
Okat, how to i define the method itself as async?
Angius
Angius5mo ago
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 it
YoItsTrev
YoItsTrevOP5mo ago
Now I am getting errors on the method name itself:
No description
Angius
Angius5mo ago
Read what I wrote again Carefully Also, read the errors Surprisingly, they tend to tell you what's wrong Wild, I know
Jimmacle
Jimmacle5mo ago
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:
YoItsTrev
YoItsTrevOP5mo ago
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:
No description
Jimmacle
Jimmacle5mo ago
you can call the method whatever you want
MODiX
MODiX5mo ago
Angius
Then, when you call this method, you will have to await it
React with ❌ to remove this embed.
Angius
Angius5mo ago
I implore you, read what is being said
Jimmacle
Jimmacle5mo ago
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
YoItsTrev
YoItsTrevOP5mo ago
No description
YoItsTrev
YoItsTrevOP5mo ago
What does that error mean?
Angius
Angius5mo ago
The method returns a Task<string> To get a string out of it, it needs to be awaited I did not tell you that the call to this method needs to be awaited for shits and giggles
YoItsTrev
YoItsTrevOP5mo ago
Still giving an error: '
No description
Jimmacle
Jimmacle5mo ago
read it think about what it's saying
Angius
Angius5mo ago
And what does the error tell you?
Jimmacle
Jimmacle5mo ago
z, don't help him he needs to figure this out this is a teaching a man to fish moment
YoItsTrev
YoItsTrevOP5mo ago
What part of "C# NOOB NEEDS HELP
Angius
Angius5mo ago
Needs help reading?
Jimmacle
Jimmacle5mo ago
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
YoItsTrev
YoItsTrevOP5mo ago
Tried turning pageSource = GetPageSource(url); to pageSource = await GetPageSource(url); still getting an error.
Jimmacle
Jimmacle5mo ago
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
YoItsTrev
YoItsTrevOP5mo ago
Fuck it..... I will just deal with the crashes if this is all the help I am gonna get.
Jimmacle
Jimmacle5mo ago
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
YoItsTrev
YoItsTrevOP5mo ago
I wil just post it on stackoverflow then.... gotta be a billion times more helpful than this.
Angius
Angius5mo ago
Oh boy :KEKW:
Jimmacle
Jimmacle5mo ago
you get out what you put in ¯\_(ツ)_/¯
jcotton42
jcotton425mo ago
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.
YoItsTrev
YoItsTrevOP5mo ago
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.
SleepWellPupper
SleepWellPupper5mo ago
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
MODiX
MODiX5mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Jester
Jester5mo ago
also dont use a new httpclient for each request. you can keep doing requests with the same one which will work better
Want results from more Discord servers?
Add your server