C# 验证http://192.168.1.220:8080////Images/心电图/2023-12/01/1222200108/0702/927aec89-db4a-4880-a0a6-fc5189047907.jpg网络路径图片是否存在
时间: 2023-12-07 19:04:28 浏览: 42
以下是C#验证网络路径图片是否存在的代码:
```csharp
using System.Net;
public bool CheckImageExists(string imageUrl)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}
catch
{
return false;
}
}
```
你可以将需要验证的图片的URL传递给CheckImageExists方法,如果返回值为true,则表示该图片存在于该URL中,否则表示该图片不存在。
--相关问题--:
1. 如何在C#中下载网络图片?
2. 如何在C#中获取网络图片的大小?
3. 如何在C#中获取网络图片的格式?
相关问题
c# http get http://61.155.88.154:7032/hb-gov/comprehensive/map/getEntPollutionNewestData/26476944
你想知道如何用 C# 发送 HTTP GET 请求并获取 http://61.155.88.154:7032/hb-gov/comprehensive/map/getEntPollutionNewestData/26476944 的响应数据吗?
可以使用 C# 的 HttpClient 类来实现。以下是一个示例代码片段:
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("http://61.155.88.154:7032/hb-gov/comprehensive/map/getEntPollutionNewestData/26476944");
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
}
}
```
这个示例使用了 HttpClient 类发送一个 GET 请求,然后读取响应的内容并打印出来。你可以根据自己的需要进行修改。
用C# 爬取http://www.cnena.com/brand/list-htm-fid-9.html 的企业数据
好的,以下是用 C# 爬取 http://www.cnena.com/brand/list-htm-fid-9.html 的企业数据的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace Crawler
{
class Program
{
static async Task Main(string[] args)
{
string url = "http://www.cnena.com/brand/list-htm-fid-9.html";
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(url);
string content = await response.Content.ReadAsStringAsync();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(content);
List<Dictionary<string, string>> companies = new List<Dictionary<string, string>>();
foreach (HtmlNode item in doc.DocumentNode.SelectNodes("//div[@class='company']"))
{
string name = item.SelectSingleNode("./h3").InnerText;
string link = item.SelectSingleNode("./a").GetAttributeValue("href", "");
string address = Regex.Match(item.InnerText, @"地址:(.*)").Groups[1].Value;
string tel = Regex.Match(item.InnerText, @"电话:(.*)").Groups[1].Value;
companies.Add(new Dictionary<string, string> { { "name", name }, { "link", link }, { "address", address }, { "tel", tel } });
}
foreach (var company in companies)
{
Console.WriteLine(company["name"]);
Console.WriteLine(company["link"]);
Console.WriteLine(company["address"]);
Console.WriteLine(company["tel"]);
Console.WriteLine();
}
}
}
}
```
这段代码使用 HttpClient 类获取网页内容,使用 HtmlAgilityPack 库解析网页,然后使用正则表达式提取相关信息。同样,你可以根据需要对代码进行修改和优化。