C
C#4mo 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
Angius4mo 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
YoItsTrevOP4mo ago
What header would I need to use, cause when I try to copy and paste the code you provided, it gives errors.
Angius
Angius4mo ago
Depends what errors those are
YoItsTrev
YoItsTrevOP4mo ago
No errors at all. The IDE is saying that the line of code is incorrect.
No description
leowest
leowest4mo 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
MODiX4mo 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
Angius4mo ago
Oof, yeah, don't use .NET Framework
YoItsTrev
YoItsTrevOP4mo 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
leowest4mo ago
I mean httpclient exists on .net framework
Angius
Angius4mo ago
Yeah, but not using statements
leowest
leowest4mo ago
sure, it would be a bit different code but still better than webclient
Angius
Angius4mo 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
leowest4mo ago
+making the method async or using GetAwaiter().GetResult()
Angius
Angius4mo 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
YoItsTrevOP4mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ Again, when I put your code in, the IDE gives me an error:
No description
Angius
Angius4mo 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
YoItsTrevOP4mo ago
Okat, how to i define the method itself as async?
Angius
Angius4mo 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
YoItsTrevOP4mo ago
Now I am getting errors on the method name itself:
No description
Angius
Angius4mo ago
Read what I wrote again Carefully Also, read the errors Surprisingly, they tend to tell you what's wrong Wild, I know
Jimmacle
Jimmacle4mo 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
YoItsTrevOP4mo 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
Jimmacle4mo ago
you can call the method whatever you want
MODiX
MODiX4mo ago
Angius
Then, when you call this method, you will have to await it
React with ❌ to remove this embed.
Angius
Angius4mo ago
I implore you, read what is being said
Jimmacle
Jimmacle4mo 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
YoItsTrevOP4mo ago
No description
YoItsTrev
YoItsTrevOP4mo ago
What does that error mean?
Angius
Angius4mo 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
YoItsTrevOP4mo ago
Still giving an error: '
No description
Jimmacle
Jimmacle4mo ago
read it think about what it's saying
Angius
Angius4mo ago
And what does the error tell you?
Jimmacle
Jimmacle4mo ago
z, don't help him he needs to figure this out this is a teaching a man to fish moment
YoItsTrev
YoItsTrevOP4mo ago
What part of "C# NOOB NEEDS HELP
Angius
Angius4mo ago
Needs help reading?
Jimmacle
Jimmacle4mo 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
YoItsTrevOP4mo ago
Tried turning pageSource = GetPageSource(url); to pageSource = await GetPageSource(url); still getting an error.
Jimmacle
Jimmacle4mo 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
YoItsTrevOP4mo ago
Fuck it..... I will just deal with the crashes if this is all the help I am gonna get.
Jimmacle
Jimmacle4mo 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
YoItsTrevOP4mo ago
I wil just post it on stackoverflow then.... gotta be a billion times more helpful than this.
Angius
Angius4mo ago
Oh boy :KEKW:
Jimmacle
Jimmacle4mo ago
you get out what you put in ¯\_(ツ)_/¯
jcotton42
jcotton424mo 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
YoItsTrevOP4mo 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
SleepWellPupper4mo 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
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Jester
Jester4mo 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