本文整理汇总了C#中WebClient.OpenRead方法的典型用法代码示例。如果您正苦于以下问题:C# WebClient.OpenRead方法的具体用法?C# WebClient.OpenRead怎么用?C# WebClient.OpenRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebClient
的用法示例。
在下文中一共展示了WebClient.OpenRead方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckForinternetConnection
private static bool CheckForinternetConnection()
{
//this might be a possible way to check for internet connection, correct if wrong.
try
{
using (var client = new WebClient())
try
{
//this will be a test to see if it can establish an internet connection
using var stream = client.OpenRead("https://www.google.com")
{
{
result = (p.Send("8.8.8.8", 15000).Status == IPStatus.Success) ||
(p.Send("8.8.4.4", 15000).Status == IPStatus.Success) ||
(p.Send("4.2.2.1", 15000).Status == IPStatus.Success);
}
}
}
catch
{
return false;
}
return result;
}
}
示例2: btnGetLoc_Click
protected void btnGetLoc_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
Stream data = wc.OpenRead(String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim()));
String str;
using (StreamReader sr = new StreamReader(data))
{
str = sr.ReadToEnd();
data.Close();
}
//String url = String.Empty;
//if (txtIP.Text.Trim() != String.Empty)
//{
// url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim());
// XDocument xDoc = XDocument.Load(url);
// if (xDoc == null | xDoc.Root == null)
// {
// throw new ApplicationException("Data is not Valid");
// }
// Xml1.TransformSource = "IP.xslt";
// Xml1.DocumentContent = xDoc.ToString();
//}
}
示例3: RetrieveScript
/// <summary>
/// Retrieves the specified remote script using a WebClient.
/// </summary>
/// <param name="file">The remote URL</param>
private static string RetrieveScript(string file)
{
string script = null;
try
{
Uri url = new Uri(file, UriKind.Absolute);
using (WebClient client = new WebClient())
using (Stream buffer = client.OpenRead(url))
using (StreamReader reader = new StreamReader(buffer))
{
script = StripWhitespace(reader.ReadToEnd());
HttpContext.Current.Cache.Insert(file, script, null, Cache.NoAbsoluteExpiration, new TimeSpan(7, 0, 0, 0));
}
}
catch (System.Net.Sockets.SocketException)
{
// The remote site is currently down. Try again next time.
}
catch (UriFormatException)
{
// Only valid absolute URLs are accepted
}
return script;
}
示例4: Main
private static void Main(string[] args)
{
if (args.Length != 2) {
Console.WriteLine("Usage: stacktrace_decoder <info_file_uri> <pdb_file>");
return;
}
string info_file_uri = args[0];
string pdb_file = args[1];
var web_client = new WebClient();
var stream = new StreamReader(web_client.OpenRead(info_file_uri));
var version_regex = new Regex(
@"^I.*\] Principia version " +
@"([0-9]{10}-[A-Za-z]+)-[0-9]+-g([0-9a-f]{40}) built");
Match version_match;
do {
version_match = version_regex.Match(stream.ReadLine());
} while (!version_match.Success);
string tag = version_match.Groups[1].ToString();
string commit = version_match.Groups[2].ToString();
var base_address_regex = new Regex(@"^I.*\] Base address is ([0-9A-F]+)$");
string base_address_string =
base_address_regex.Match(stream.ReadLine()).Groups[1].ToString();
Int64 base_address = Convert.ToInt64(base_address_string, 16);
var stack_regex = new Regex(
@"^\[email protected]\s+[0-9A-F]+\s+\(No symbol\) \[0x([0-9A-F]+)\]");
Match stack_match;
do {
stack_match = stack_regex.Match(stream.ReadLine());
} while (!stack_match.Success);
var file_regex = new Regex(
@"file\s+:\s+.*\\principia\\([a-z_]+)\\([a-z_]+\.[ch]pp)");
var line_regex = new Regex(@"line\s+:\s+([0-9]+)");
for (;
stack_match.Success;
stack_match = stack_regex.Match(stream.ReadLine())) {
Int64 address = Convert.ToInt64(stack_match.Groups[1].ToString(), 16);
Int64 dbh_base_address = 0x1000000;
string rebased_address =
Convert.ToString(address - base_address + dbh_base_address, 16);
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = kDBH;
p.StartInfo.Arguments =
'"' + pdb_file + "\" laddr \"" + rebased_address + '"';
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Match file_match = file_regex.Match(output);
if (file_match.Success) {
string file = file_match.Groups[1].ToString() + '/' +
file_match.Groups[2].ToString();
string line = line_regex.Match(output).Groups[1].ToString();
string url = "https://github.com/mockingbirdnest/Principia/blob/" +
commit + '/' + file + "#L" + line;
Console.WriteLine("[`" + file + ":" + line + "`](" + url + ")");
}
}
}
示例5: Main
public static void Main()
{
WebClient cl = new WebClient();
Stream str = cl.OpenRead("http://www.google.com");
StreamReader read = new StreamReader(str);
Console.WriteLine(read.ReadToEnd());
str.Close();
}
示例6: getPage
private static String getPage(String url)
{
using(WebClient cl = new WebClient())
using(Stream data = cl.OpenRead(url))
using(StreamReader r = new StreamReader(data))
{
return r.ReadToEnd();
}
}
示例7: CheckForInternetConnection
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}
示例8: Main
static void Main()
{
using (WebClient download = new WebClient())
{
try
{
download.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "../../AC.jpg");
download.OpenRead("http://www.devbg.org/img/Logo-BASD.jpg");
}
catch (Exception Ex)
{
Console.WriteLine("invalid address!",Ex.Data);
}
}
}
示例9: ReadHtml
/// <summary>
/// 读取网页内容
/// </summary>
/// <returns></returns>
public static string ReadHtml()
{
WebClient client = new WebClient();
Stream strm = client.OpenRead("http://www.baidu.com");
StreamReader sr = new StreamReader(strm);
string line = string.Empty;
string message = string.Empty;
while ((line = sr.ReadLine()) != null)
{
message += line;
}
strm.Close();
return message;
}
示例10: SendMessageWithHttpStream
static void SendMessageWithHttpStream(IBus bus)
{
#region send-message-with-http-stream
using (WebClient webClient = new WebClient())
{
MessageWithStream message = new MessageWithStream
{
SomeProperty = "This message contains a stream",
StreamProperty = webClient.OpenRead("http://www.particular.net")
};
bus.Send("Sample.PipelineStream.Receiver", message);
}
#endregion
Console.WriteLine();
Console.WriteLine("Message with http stream sent");
}
示例11: API
public static string API(string API)
{
try
{
WebClient Client = new WebClient();
string baseurl = API;
Stream data = Client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
示例12: Main
public static void Main(String [] args)
{
WebClient client = new WebClient();
client.BaseAddress = args[0];
client.DownloadFile(args[1], args[2]);
StreamReader input =
new StreamReader(client.OpenRead(args[1]));
Console.WriteLine(input.ReadToEnd());
Console.WriteLine
("Request header count: {0}", client.Headers.Count);
WebHeaderCollection header = client.ResponseHeaders;
Console.WriteLine
("Response header count: {0}", header.Count);
for (int i = 0; i < header.Count; i++)
Console.WriteLine(" {0} : {1}",
header.GetKey(i), header[i]);
input.Close();
}
示例13: GetLocation
public string GetLocation()
{
string wholePage = string.Empty;
try
{
using (WebClient webClient = new WebClient())
{
Stream stream = webClient.OpenRead("https://freegeoip.net/json/");
StreamReader streamReader = new StreamReader(stream);
wholePage = streamReader.ReadToEnd();
streamReader.Close();
stream.Close();
}
return wholePage;
}
catch(Exception e)
{
return e.ToString();
}
}
示例14: Main
static void Main()
{
using (WebClient client = new WebClient())
{
try
{
// Reads some file address in Internet
Console.WriteLine("Please, enter some file address in Internet: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string uri = Console.ReadLine();
Console.ResetColor();
// Takes the name of the file
Match filename = Regex.Match(uri, @"[^\/]+\.\w+$");
// Checks the size of the file before to be downloaded
client.OpenRead(uri);
Int64 bytes = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
Console.Write("\nThe size of the file is " + bytes.ToString() + " Bytes. Press <Enter> to start downloading");
Console.ReadLine();
// Downloads the file
client.DownloadFile(uri, filename.ToString());
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("The file is successfully downloaded!");
Console.ResetColor();
}
catch (WebException)
{
Error("File not found!");
}
catch (ArgumentException)
{
Error("The path is not of a legal form!");
}
catch (PathTooLongException)
{
Error("The specified path, file name, or both are too long!");
}
}
}
示例15: ReadFile
public string ReadFile()
{
String fileUrl = "http://marketinvoice.looker.com/looks/XN9ChdBFx4KMSBTMPzQK6P6k6p9FJGWv.txt";
WebClient client = new WebClient();
Stream data = client.OpenRead(fileUrl);
StreamReader reader = new StreamReader(data);
string text = reader.ReadToEnd();
string[] contentCollection = text.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
int length = contentCollection.Length;
string result = "[ ";
/** Headers **/
string[] headers = new string[] { };
int count = 0;
string firstLine = contentCollection[0];
if (firstLine != null)
{
headers = firstLine.Replace(" ", "").Replace("%", "Percent").Replace("(", "").Replace(")", "").Split('\t');
count = headers.Length;
}
/** Values **/
string[] row = new string[] { };
for (int countLines = 1; countLines < length; countLines++)
{
row = contentCollection[countLines].Split('\t');
result += "{";
for (int countValues = 0; countValues < count; countValues++)
{
result += String.Format("{0}: \"{1}\", ", headers[countValues], row[countValues]);
}
result += "}, ";
}
result = result.Replace(@"\", "");
result = result.Remove(result.Length - 1);
result += "]";
return result;
}