本文整理汇总了C#中System.Net.WebClient.OpenRead方法的典型用法代码示例。如果您正苦于以下问题:C# System.Net.WebClient.OpenRead方法的具体用法?C# System.Net.WebClient.OpenRead怎么用?C# System.Net.WebClient.OpenRead使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebClient
的用法示例。
在下文中一共展示了System.Net.WebClient.OpenRead方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetWebServiceAssembly
/// <summary>
/// get an Assembly according to wsdl path.
/// </summary>
/// <param name="wsdl">wsdl path</param>
/// <param name="nameSpace">namespace</param>
/// <returns>return Assembly</returns>
public static Assembly GetWebServiceAssembly(string wsdl, string nameSpace)
{
try
{
System.Net.WebClient webClient = new System.Net.WebClient();
System.IO.Stream webStream = webClient.OpenRead(wsdl);
ServiceDescription serviceDescription = ServiceDescription.Read(webStream);
ServiceDescriptionImporter serviceDescroptImporter = new ServiceDescriptionImporter();
serviceDescroptImporter.AddServiceDescription(serviceDescription, "", "");
System.CodeDom.CodeNamespace codeNameSpace = new System.CodeDom.CodeNamespace(nameSpace);
System.CodeDom.CodeCompileUnit codeCompileUnit = new System.CodeDom.CodeCompileUnit();
codeCompileUnit.Namespaces.Add(codeNameSpace);
serviceDescroptImporter.Import(codeNameSpace, codeCompileUnit);
System.CodeDom.Compiler.CodeDomProvider codeDom = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters codeParameters = new System.CodeDom.Compiler.CompilerParameters();
codeParameters.GenerateExecutable = false;
codeParameters.GenerateInMemory = true;
codeParameters.ReferencedAssemblies.Add("System.dll");
codeParameters.ReferencedAssemblies.Add("System.XML.dll");
codeParameters.ReferencedAssemblies.Add("System.Web.Services.dll");
codeParameters.ReferencedAssemblies.Add("System.Data.dll");
System.CodeDom.Compiler.CompilerResults compilerResults = codeDom.CompileAssemblyFromDom(codeParameters, codeCompileUnit);
return compilerResults.CompiledAssembly;
}
catch (Exception ex)
{
throw ex;
}
}
示例2: GetInstallFileStream
public Stream GetInstallFileStream(string packageFileDownloadUrl)
{
Log.LogVerbose("PackageServerFacade", string.Format("Downloading file: {0}", packageFileDownloadUrl));
var client = new System.Net.WebClient();
return client.OpenRead(packageFileDownloadUrl);
}
示例3: GetStopTimes
public override List<StopTime> GetStopTimes(string stopID)
{
System.Net.WebClient client = new System.Net.WebClient();
DataSet ds = new DataSet();
ds.ReadXml(client.OpenRead("http://www.ctabustracker.com/bustime/api/v1/getpredictions?key=" + APIKey + "&stpid=" + stopID));
List<StopTime> result = new List<StopTime>();
if (ds.Tables["prd"] == null)
{
return result;
}
foreach (DataRow r in ds.Tables["prd"].Rows)
{
StopTime t = new StopTime();
t.RouteShortName = r["rt"].ToString();
t.RouteLongName = r["rt"].ToString();
t.ArrivalTime = DateTime.ParseExact(r["prdtm"].ToString(), "yyyyMMdd HH:mm", new System.Globalization.CultureInfo("en-US")).ToString("hh:mm tt");
t.DepartureTime = t.ArrivalTime;
t.Type = "realtime";
if ((from x in result where x.RouteShortName == t.RouteShortName select x).Count() < 2)
result.Add(t);
}
return result;
}
示例4: backgroundWorker1_DoWork
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// the URL to download the file from
string sUrlToReadFileFrom = textBoxSourceFile.Text;
// the path to write the file to
int iLastIndex = sUrlToReadFileFrom.LastIndexOf('/');
string sDownloadFileName = sUrlToReadFileFrom.Substring(iLastIndex + 1, (sUrlToReadFileFrom.Length - iLastIndex - 1));
string sFilePathToWriteFileTo = textBoxDestinationFolder.Text + "\\" + sDownloadFileName;
// first, we need to get the exact size (in bytes) of the file we are downloading
Uri url = new Uri(sUrlToReadFileFrom);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
// gets the size of the file in bytes
Int64 iSize = response.ContentLength;
// keeps track of the total bytes downloaded so we can update the progress bar
Int64 iRunningByteTotal = 0;
// use the webclient object to download the file
using (System.Net.WebClient client = new System.Net.WebClient())
{
// open the file at the remote URL for reading
using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
{
// using the FileStream object, we can write the downloaded bytes to the file system
using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
{
// loop the stream and get the file into the byte buffer
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
// write the bytes to the file system at the file path specified
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
// calculate the progress out of a base "100"
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
backgroundWorker1.ReportProgress(iProgressPercentage);
}
// clean up the file stream
streamLocal.Close();
}
// close the connection to the remote server
streamRemote.Close();
}
}
}
示例5: GetOne
public GeoSuggestion GetOne(string term)
{
if (!string.IsNullOrEmpty(term))
{
var key = Regex.Replace(term, @"[\s\.,;:]", "");
var cache = new Cahching.Cache<GeoSuggestion>();
var geoSug = cache.Get(key);
if (geoSug != null)
return geoSug;
System.Net.WebClient client = new System.Net.WebClient();
// Add a user agent header in case the
// requested URI contains a query.
//client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.QueryString.Add("input", term);
client.QueryString.Add("sensor", "false");
client.QueryString.Add("types", "geocode");
client.QueryString.Add("key", ConfigurationManager.AppSettings["GoogleAPIKey"]);
System.IO.Stream data = client.OpenRead("https://maps.googleapis.com/maps/api/place/autocomplete/xml");
System.IO.StreamReader reader = new System.IO.StreamReader(data);
string str = reader.ReadToEnd();
data.Close();
reader.Close();
XDocument xdoc = XDocument.Parse(str);
var status = xdoc.Root.Descendants("status").Single();
if (status.Value == "OK")
{
var suggestions = xdoc.Root.Descendants("prediction")
//.Where(p => p.Descendants("type").Any(s => s.Value == "geocode" || s.Value == "route"))
.Select(p => new Suggestion
{
id = p.Descendants("id").Single().Value,
descr = p.Descendants("description").Single().Value,
refer = p.Descendants("reference").Single().Value,
term = p.Descendants("term").Descendants("value").First().Value,
matches = p.Descendants("matched_substring").Skip(2).Select(s => new MatchedSubstring {
offset = int.Parse(s.Descendants("offset").Single().Value),
length = int.Parse(s.Descendants("length").Single().Value)}).ToArray()
}).ToArray();
geoSug = new GeoSuggestion { term = term, suggestions = suggestions };
cache.Set(key, geoSug);
return geoSug;
}
}
return new GeoSuggestion { term = term, suggestions = new Suggestion[0] };
}
示例6: Generate
public void Generate()
{
if (this.AddressType == AddressType.WebService)
{
string className = MethodHelper.GetClassName(this.Address);
//Get wsdl information
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream strm = wc.OpenRead(Address + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(strm);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(this.ComileNamespace);
//Generate client proxy class
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler();
//Setting compile parameters
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateExecutable = false;
compilerParams.GenerateInMemory = true;
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.ReferencedAssemblies.Add("System.XML.dll");
compilerParams.ReferencedAssemblies.Add("System.Web.Services.dll");
compilerParams.ReferencedAssemblies.Add("System.Data.dll");
//compile agentcy class
CompilerResults cr = icc.CompileAssemblyFromDom(compilerParams, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
this.ProxyAssemble = cr.CompiledAssembly;
Type t = this.ProxyAssemble.GetType(string.Concat(this.ComileNamespace, ".", className), true, true);
ServiceObject = Activator.CreateInstance(t);
}
else
{
ServiceObject = new NormalRequest();
}
SetUrl(this.Address);
}
示例7: GetWebServiceAssembly
/// <summary>
/// 根据WEBSERVICE地址获取一个 Assembly
/// </summary>
/// <param name="p_Url">地址</param>
/// <param name="p_NameSpace">命名空间</param>
/// <returns>返回Assembly</returns>
public static Assembly GetWebServiceAssembly(string p_Url, string p_NameSpace)
{
try
{
System.Net.WebClient _WebClient = new System.Net.WebClient();
System.IO.Stream _WebStream = _WebClient.OpenRead(p_Url);
ServiceDescription _ServiceDescription = ServiceDescription.Read(_WebStream);
_WebStream.Close();
_WebClient.Dispose();
ServiceDescriptionImporter _ServiceDescroptImporter = new ServiceDescriptionImporter();
_ServiceDescroptImporter.AddServiceDescription(_ServiceDescription, "", "");
System.CodeDom.CodeNamespace _CodeNameSpace = new System.CodeDom.CodeNamespace(p_NameSpace);
System.CodeDom.CodeCompileUnit _CodeCompileUnit = new System.CodeDom.CodeCompileUnit();
_CodeCompileUnit.Namespaces.Add(_CodeNameSpace);
_ServiceDescroptImporter.Import(_CodeNameSpace, _CodeCompileUnit);
System.CodeDom.Compiler.CodeDomProvider _CodeDom = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters _CodeParameters = new System.CodeDom.Compiler.CompilerParameters();
_CodeParameters.GenerateExecutable = false;
_CodeParameters.GenerateInMemory = true;
_CodeParameters.ReferencedAssemblies.Add("System.dll");
_CodeParameters.ReferencedAssemblies.Add("System.XML.dll");
_CodeParameters.ReferencedAssemblies.Add("System.Web.Services.dll");
_CodeParameters.ReferencedAssemblies.Add("System.Data.dll");
System.CodeDom.Compiler.CompilerResults _CompilerResults = _CodeDom.CompileAssemblyFromDom(_CodeParameters, _CodeCompileUnit);
if (_CompilerResults.Errors.HasErrors)
{
string _ErrorText = "";
foreach (CompilerError _Error in _CompilerResults.Errors)
{
_ErrorText += _Error.ErrorText + "/r/n";
}
throw new Exception(_ErrorText);
}
return _CompilerResults.CompiledAssembly;
}
catch (Exception ex)
{
throw ex;
}
}
示例8: CheckForInternetConnection
private static bool CheckForInternetConnection()
{
try
{
using (var client = new System.Net.WebClient())
{
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
}
catch
{
return false;
}
}
示例9: FileExists
private bool FileExists(string URL)
{
bool result = false;
using (System.Net.WebClient client = new System.Net.WebClient())
{
try
{
System.IO.Stream str = client.OpenRead(URL);
if (str != null) result = true; else result = false;
}
catch
{
result = false;
}
}
return result;
}
示例10: CheckForUpdates
public static void CheckForUpdates()
{
// Get the installation path
DirectoryInfo installationPath = GetInstallationPath();
XmlSerializer deserializer = new XmlSerializer(typeof(ButtonConfiguration));
ButtonConfiguration buttonConfig;
// Iterate over Add-ins found
DirectoryInfo buttonRoot = new DirectoryInfo(Path.Combine(installationPath.FullName, "Buttons"));
DirectoryInfo[] buttons = buttonRoot.GetDirectories();
foreach (FileInfo file in buttons.Select(button => new FileInfo(Path.Combine(button.FullName, "config.xml"))))
{
// open the configuration file for the button
using (FileStream buttonStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
{
buttonConfig = (ButtonConfiguration)deserializer.Deserialize(buttonStream);
}
// if an onlineUrl is present, then we look for an update
if (!string.IsNullOrEmpty(buttonConfig.onlineUrl))
{
string currentMenu;
using (TextReader tr = new StreamReader(Path.Combine(file.DirectoryName, @"button.xml")))
{
currentMenu = tr.ReadToEnd();
}
using (System.Net.WebClient client = new System.Net.WebClient())
{
Stream myStream = client.OpenRead(buttonConfig.onlineUrl);
using (StreamReader sr = new StreamReader(myStream))
{
string latestMenu = sr.ReadToEnd();
if (latestMenu != currentMenu)
{
using (TextWriter tw = new StreamWriter(Path.Combine(file.DirectoryName, @"button.xml")))
{
tw.Write(latestMenu);
}
}
}
}
}
}
}
示例11: ParseAnimePreviewFromWeb
public Dictionary<string, string> ParseAnimePreviewFromWeb(string url)
{
/*var web = new HtmlWeb ();
web.AutoDetectEncoding = false;
web.OverrideEncoding = System.Text.Encoding.GetEncoding ("windows-1251");
var doc = web.Load (url);
return ParseAnimePreview (doc);*/
var document = new HtmlDocument();
using (var client = new System.Net.WebClient())
{
using (var stream = client.OpenRead(url))
{
var reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("windows-1251"));
var html = reader.ReadToEnd();
document.LoadHtml(html);
}
}
return ParseAnimePreview(document);
}
示例12: Collect
/// <summary>
/// Beszerzi a legfrisebb térképet, szétparszolja, és elrakja
/// ToDo: Csak akkor kéne elrakni, ha különbözik az előzőtől
/// Ijen egy sor:
/// INSERT INTO `x_world` VALUES (188644,8,165,1,45770,'Mr G faluja',30778,'Mr G',0,'',3);
/// </summary>
/// <param name="url">Innen töltike lefelé</param>
/// <param name="Data">Ide kell elrakni</param>
public static void Collect(string url, TraviData Data)
{
System.Net.WebClient Client = new System.Net.WebClient();
Stream strm = Client.OpenRead(url);
StreamReader sr = new StreamReader(strm);
string line;
string[] details;
DateTime now = DateTime.Now;
do
{
line = sr.ReadLine();
if (line != null)
{
line = line.Substring(30, line.Length - 32);
details = line.Split(',');
MapElement me = new MapElement();
me.TimeStamp = now;
try
{
me.Id = int.Parse(details[0]);
me.X = int.Parse(details[1]);
me.Y = int.Parse(details[2]);
me.Tid = int.Parse(details[3]);
me.Vid = int.Parse(details[4]);
me.Village = details[5].Trim('\'');
me.Uid = int.Parse(details[6]);
me.Player = details[7].Trim('\'');
me.Aid = int.Parse(details[8]);
me.Alliance = details[9].Trim('\'');
me.Population = int.Parse(details[10]);
}
catch { }
Data.Map.Add(me);
}
}
while (line != null);
strm.Close();
}
示例13: GetStopTimes
public override List<StopTime> GetStopTimes(string stopid)
{
System.Net.WebClient client = new System.Net.WebClient();
GTFS engine = new GTFS(this.TransitAgency);
Stop stop = engine.GetStop(stopid);
DataSet ds = new DataSet();
ds.ReadXml(client.OpenRead("http://myride.gocitybus.com/widget/Default1.aspx?pt=30&code=" + stop.Code));
List<StopTime> result = new List<StopTime>();
if (ds.Tables["Bus"] == null)
{
return result;
}
foreach (DataRow r in ds.Tables["Bus"].Rows)
{
StopTime t = new StopTime();
t.RouteShortName = r["RouteName"].ToString().Substring(0, r["RouteName"].ToString().IndexOf(" ")).ToUpper().Trim(); if (t.RouteShortName.Length > 3) t.RouteShortName = t.RouteShortName.Substring(0, 3);
t.RouteLongName = r["RouteName"].ToString().Replace(t.RouteShortName, "").Trim();
var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, this.TransitAgency.FriendlyTimeZone);
if (r["TimeTillArrival"].ToString() == "DUE")
t.ArrivalTime = now.ToString("hh:mm tt");
else
t.ArrivalTime = now.AddMinutes(Convert.ToInt32(r["TimeTillArrival"].ToString().Replace("min", "").Trim())).ToString("hh:mm tt");
t.DepartureTime = t.ArrivalTime;
t.Type = "realtime";
result.Add(t);
}
return result;
}
示例14: GetStopTimes
public override List<StopTime> GetStopTimes(string stopID)
{
System.Net.WebClient client = new System.Net.WebClient();
DataSet ds = new DataSet();
ds.ReadXml(client.OpenRead("http://api.bart.gov/api/etd.aspx?cmd=etd&key=" + APIKey + "&orig=" + stopID));
List<StopTime> result = new List<StopTime>();
foreach (DataRow r in ds.Tables["etd"].Rows)
{
foreach (DataRow s in ds.Tables["estimate"].Rows)
{
if (s["etd_Id"].ToString() == r["etd_Id"].ToString())
{
StopTime t = new StopTime();
t.RouteShortName = r["abbreviation"].ToString();
t.RouteLongName = r["destination"].ToString();
var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, this.TransitAgency.FriendlyTimeZone);
if (s["minutes"].ToString() == "Arrived")
t.ArrivalTime = now.ToString("hh:mm tt");
else
t.ArrivalTime = now.AddMinutes(Convert.ToInt32(s["minutes"].ToString().Trim())).ToString("hh:mm tt");
t.DepartureTime = t.ArrivalTime;
t.Type = "realtime";
if ((from x in result where x.RouteShortName == t.RouteShortName select x).Count() < 2)
result.Add(t);
}
}
}
return result;
}
示例15: Main
static void Main(string[] args)
{
//String url = "http://www.yelp.com/biz/bacchanalia-atlanta";
//String url = "http://www.zagat.com/r/bacchanalia-atlanta";
String url = "http://www.urbanspoon.com/r/9/120002/restaurant/Westside/Bacchanalia-Atlanta";
htmlDoc = new HtmlDocument();
using (System.Net.WebClient webClient = new System.Net.WebClient())
{
using (var stream = webClient.OpenRead(url))
{
htmlDoc.Load(stream);
}
}
if (url.Contains("dine"))
{
DineScraper dine = new DineScraper(url, htmlDoc, "dine");
}
else if (url.Contains("google"))
{
GoogleScraper google = new GoogleScraper(url, htmlDoc, "google");
}
else if (url.Contains("urbanspoon"))
{
UrbanspoonScraper urbanspoon = new UrbanspoonScraper(url, htmlDoc, "urbanspoon");
}
else if (url.Contains("yelp"))
{
YelpScraper yelp = new YelpScraper(url, htmlDoc, "yelp");
}
else if (url.Contains("zagat"))
{
ZagatScraper zagat = new ZagatScraper(url, htmlDoc, "zagat");
}
}