libcurl in C# and .NET
I’m a PHP developer, my current job is maintaining a few web sites. So I have some free time and thoughts about developing some GUI applications using the c# language and .NET platform. Implementation of this applications requires libcURL functiona开发者_开发问答lity.
Are there any .net classes or third-party libs in c#, that can provide me with the same functionality as libcurl in PHP ?
Sorry for my bad English…
Yes, the HttpWebRequest
and HttpWebResponse
classes make it amazingly simple to grab web content from a C# app.
Here's a little snippet of code where I was grabbing a clock time from a webpage:
private const string URL = "http://housing.udayton.edu/php/lottery/clock.php";
private const string StartString = "<p align=\"center\"><b>";
private const string EndString = "</b>";
private DateTime getTime()
{
HttpWebResponse resp = HttpWebRequest.Create(URL).GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(resp.GetResponseStream());
String data = reader.ReadToEnd();
int startIndex = data.IndexOf(StartString) + StartString.Length;
int endIndex = data.IndexOf(EndString, startIndex);
String time = data.Substring(startIndex, (endIndex-startIndex)-4);
return DateTime.Parse(time);
}
Here's a good article that will help you as well:
http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
Of course, nothing is stopping you from actually using libcurl in your .NET application:
http://curl.haxx.se/libcurl/dotnet/
You're looking for the WebClient
class, and perhaps HttpWebRequest
.
精彩评论