本文整理汇总了C#中System.Net.WebClient类的典型用法代码示例。如果您正苦于以下问题:C# WebClient类的具体用法?C# WebClient怎么用?C# WebClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebClient类属于System.Net命名空间,在下文中一共展示了WebClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindBooksByOLIDs
public List<BookData> FindBooksByOLIDs(List<string> oLIDs)
{
if (oLIDs == null)
{
throw new ArgumentNullException("oLIDs");
}
List<BookData> books = new List<BookData>();
var bibkeys = new StringBuilder();
foreach (var oLID in oLIDs)
{
bibkeys.Append(oLID);
bibkeys.Append(",");
}
if (bibkeys.Length != 0)
{
var getUri = baseUrl + "books?bibkeys=OLID:" + bibkeys.ToString().TrimEnd(new char[] { ',' }) + "&format=json&jscmd=data";
using (var webClient = new WebClient())
{
var response = JsonConvert.DeserializeObject<Dictionary<string, BookData>>(webClient.DownloadString(getUri));
if (response.Count() > 0)
{
books = new List<BookData>();
foreach (var value in response.Values)
{
books.Add(value);
}
}
}
}
return books;
}
示例2: LoadXml
/// <summary>
/// loads a xml from the web server
/// </summary>
/// <param name="_url">URL of the XML file</param>
/// <returns>A XmlDocument object of the XML file</returns>
public static XmlDocument LoadXml(string _url)
{
var xmlDoc = new XmlDocument();
try
{
while (Helper.pingForum("forum.mods.de", 10000) == false)
{
Console.WriteLine("Can't reach forum.mods.de right now, try again in 15 seconds...");
System.Threading.Thread.Sleep(15000);
}
xmlDoc.Load(_url);
}
catch (XmlException)
{
while (Helper.pingForum("forum.mods.de", 100000) == false)
{
Console.WriteLine("Can't reach forum.mods.de right now, try again in 15 seconds...");
System.Threading.Thread.Sleep(15000);
}
WebClient client = new WebClient(); ;
Stream stream = client.OpenRead(_url);
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
content = RemoveTroublesomeCharacters(content);
xmlDoc.LoadXml(content);
}
return xmlDoc;
}
示例3: GetVersion
public async override Task<IEnumerable<string>> GetVersion(string packageName)
{
if (string.IsNullOrEmpty(packageName))
return Enumerable.Empty<string>();
string url = $"http://registry.npmjs.org/{packageName}";
string json = "{}";
using (var client = new WebClient())
{
json = await client.DownloadStringTaskAsync(url);
}
var array = JObject.Parse(json);
var time = array["time"];
if (time == null)
return Enumerable.Empty<string>();
var props = time.Children<JProperty>();
return from version in props
where char.IsNumber(version.Name[0])
orderby version.Name descending
select version.Name;
}
示例4: Search
public List<SearchResult> Search(string searchQuery)
{
string url = "http://www.imdb.com/find?q={0}&s=all".Fmt(searchQuery);
string html = new WebClient().DownloadString(url);
CQ dom = html;
var searchResults = new List<SearchResult>();
foreach (var fragment in dom.Select("table.findList tr.findResult"))
{
var searchResult = new SearchResult();
searchResult.ImageUrl = fragment.Cq().Find(".primary_photo > a > img").Attr("src");
searchResult.Text = fragment.Cq().Find(".result_text").Html();
searchResult.Text = StringEx.RemoveHtmlTags(searchResult.Text);
string filmUrl = fragment.Cq().Find(".result_text > a").Attr("href");
filmUrl = filmUrl.Replace("/title/", "");
searchResult.FilmId = filmUrl.Substring(0, filmUrl.IndexOf("/"));
searchResults.Add(searchResult);
}
return searchResults;
}
示例5: ItemCrawler
public ItemCrawler(Uri url)
{
_htmlDocument = new HtmlDocument();
var html = new WebClient().DownloadString(url.OriginalString);
_htmlDocument.LoadHtml(html);
_document = _htmlDocument.DocumentNode;
}
示例6: DownloadQueue
public async Task<Queue> DownloadQueue()
{
using (WebClient w = new WebClient())
{
return this.ConvertApiResultToQueue(await w.DownloadStringTaskAsync("http://localhost:8080/sabnzbd/api?mode=queue&output=xml"));
}
}
示例7: GetCoinInformation
public static List<CoinInformation> GetCoinInformation(string userAgent = "",
BaseCoin profitabilityBasis = BaseCoin.Bitcoin)
{
WebClient client = new WebClient();
if (!string.IsNullOrEmpty(userAgent))
client.Headers.Add("user-agent", userAgent);
string apiUrl = GetApiUrl(profitabilityBasis);
string jsonString = client.DownloadString(apiUrl);
JArray jsonArray = JArray.Parse(jsonString);
List<CoinInformation> result = new List<CoinInformation>();
foreach (JToken jToken in jsonArray)
{
CoinInformation coinInformation = new CoinInformation();
coinInformation.PopulateFromJson(jToken);
if (coinInformation.Difficulty > 0)
//only add coins with valid info since the user may be basing
//strategies on Difficulty
result.Add(coinInformation);
}
return result;
}
示例8: HttpClient
public HttpClient (Uri baseUrl)
{
// this.baseUrl = baseUrl;
visualizeUrl = new Uri (baseUrl, "visualize");
stopVisualizingUrl = new Uri (baseUrl, "stopVisualizing");
client = new WebClient ();
}
示例9: TranslateString
/// <summary>
/// Used to perform the actual conversion of the input string
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public override string TranslateString(string InputString)
{
Console.WriteLine("Processing: " + InputString);
string result = "";
using (WebClient client = new WebClient())
{
using (Stream data = client.OpenRead(this.BuildRequestString(InputString)))
{
using (StreamReader reader = new StreamReader(data))
{
string s = reader.ReadToEnd();
result = ExtractTranslatedString(s);
reader.Close();
}
data.Close();
}
}
return result;
}
示例10: UpdateFiles
public void UpdateFiles()
{
try
{
WriteLine("Local version: " + Start.m_Version);
WebClient wc = new WebClient();
string versionstr;
using(System.IO.StreamReader sr = new System.IO.StreamReader(wc.OpenRead(baseurl + "version.txt")))
{
versionstr = sr.ReadLine();
}
Version remoteversion = new Version(versionstr);
WriteLine("Remote version: " + remoteversion);
if(Start.m_Version < remoteversion)
{
foreach(string str in m_Files)
{
WriteLine("Updating: " + str);
wc.DownloadFile(baseurl + str, str);
}
}
wc.Dispose();
WriteLine("Update complete");
}
catch(Exception e)
{
WriteLine("Update failed:");
WriteLine(e);
}
this.Button_Ok.Enabled = true;
}
示例11: AddNewGameHistory
public GamePlayHistory AddNewGameHistory(GamePlayHistory gamePlayHistory)
{
if (string.IsNullOrEmpty(ToSavourToken))
{
throw new InvalidOperationException("No ToSavour Token is set");
}
RequestClient = new WebClient();
RequestClient.Headers.Add("Authorization", ToSavourToken);
RequestClient.Headers.Add("Content-Type", "application/json");
var memoryStream = new MemoryStream();
GetSerializer(typeof(GamePlayHistory)).WriteObject(memoryStream, gamePlayHistory);
memoryStream.Position = 0;
var sr = new StreamReader(memoryStream);
var json = sr.ReadToEnd();
var userJsonString = RequestClient.UploadString(_host + @"gamehistories", json);
var byteArray = Encoding.ASCII.GetBytes(userJsonString);
var stream = new MemoryStream(byteArray);
var returnedGamePlayHistory = GetSerializer(typeof(GamePlayHistory)).ReadObject(stream) as GamePlayHistory;
return returnedGamePlayHistory;
}
示例12: CreateIncident
private void CreateIncident()
{
WebClient client = new WebClient();
client.Headers[HttpRequestHeader.Accept] = "application/json";
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.UploadStringCompleted += (object source, UploadStringCompletedEventArgs e) =>
{
if (e.Error != null || e.Cancelled)
{
Console.WriteLine("Error" + e.Error);
Console.ReadKey();
}
};
JavaScriptSerializer js = new JavaScriptSerializer();
TriggerDetails triggerDetails = new TriggerDetails(Component, Details);
var detailJson = js.Serialize(triggerDetails);
//Alert name should be unique for each alert - as alert name is used as incident key in pagerduty.
string key = ConfigurationManager.AppSettings["PagerDutyServiceKey"];
if (!string.IsNullOrEmpty(EscPolicy))
{
key = ConfigurationManager.AppSettings["PagerDutySev1ServiceKey"];
}
if (string.IsNullOrEmpty(key))
{
key = ConfigurationManager.AppSettings["PagerDutyServiceKey"];
}
Trigger trigger = new Trigger(key,AlertName,AlertSubject,detailJson);
var triggerJson = js.Serialize(trigger);
client.UploadString(new Uri("https://events.pagerduty.com/generic/2010-04-15/create_event.json"), triggerJson);
}
示例13: UserAuthenticate
protected void UserAuthenticate(object sender, AuthenticateEventArgs e)
{
if (Membership.ValidateUser(this.LoginForm.UserName, this.LoginForm.Password))
{
e.Authenticated = true;
return;
}
string url = string.Format(
this.ForumAuthUrl,
HttpUtility.UrlEncode(this.LoginForm.UserName),
HttpUtility.UrlEncode(this.LoginForm.Password)
);
WebClient web = new WebClient();
string response = web.DownloadString(url);
if (response.Contains(groupId))
{
e.Authenticated = Membership.ValidateUser("Premier Subscriber", "danu2HEt");
this.LoginForm.UserName = "Premier Subscriber";
HttpCookie cookie = new HttpCookie("ForumUsername", this.LoginForm.UserName);
cookie.Expires = DateTime.Now.AddMonths(2);
Response.Cookies.Add(cookie);
}
}
示例14: FindOLIDsByTitle
public List<string> FindOLIDsByTitle(string title)
{
if (string.IsNullOrEmpty(title))
{
throw new ArgumentNullException("title");
}
// OpenLibrary is friendly with the book title whose first character is captial.
title = title[0].ToString().ToUpper() + title.Substring(1);
var uri = baseUrl + "things?query={\"type\":\"\\/type\\/edition\",\"title~\":\"" + title + "*\"}&prettyprint=true&text=true";
List<string> oLIDs = new List<string>();
using (var webClient = new WebClient())
{
var response = JsonConvert.DeserializeObject<Thing>(webClient.DownloadString(uri));
if (response.Status.Equals("ok", StringComparison.OrdinalIgnoreCase))
{
foreach (var oLID in response.Result)
{
string strToRemove = "/books/";
if (oLID.StartsWith(strToRemove))
{
oLIDs.Add(oLID.Replace(strToRemove, ""));
}
}
}
}
return oLIDs;
}
示例15: DELETE
public static string DELETE(string uri, IDictionary<string, string> args)
{
try {
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Headers["Connection"] = "Keep-Alive";
StringBuilder formattedParams = new StringBuilder();
IDictionary<string, string> parameters = new Dictionary<string, string>();
foreach (var arg in args)
{
parameters.Add(arg.Key, arg.Value);
formattedParams.AppendFormat("{0}={{{1}}}", arg.Key, arg.Key);
}
//Formatted URI
Uri baseUri = new Uri(uri);
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompletedDelete);
client.UploadStringAsync(baseUri, "DELETE", string.Empty);
allDone.Reset();
allDone.WaitOne();
return requestResult;
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}