/// <summary>
/// 判断远程图片是否存在
/// </summary>
/// <param name="curl">远程图片地址</param>
/// <returns></returns>
private int GetUrlError(string curl)
{
int num = 200;
if (this.method == 1)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(curl));
ServicePointManager.Expect100Continue = false;
try
{
((HttpWebResponse)request.GetResponse()).Close();
}
catch (WebException exception)
{
if (exception.Status != WebExceptionStatus.ProtocolError)
{
return num;
}
if (exception.Message.IndexOf("500 ") > 0)
{
return 500;
}
if (exception.Message.IndexOf("401 ") > 0)
{
return 401;
}
if (exception.Message.IndexOf("404") > 0)
{
num = 404;
}
}
return num;
}
}
/*用第二个方法,获取远程文件的大小*/
/// 判断远程文件是否存在
/// fileUrl:远程文件路径,包括IP地址以及详细的路径
/// <summary>
///
/// </summary>
/// <param name="fileUrl"></param>
/// <returns></returns>
private bool RemoteFileExists(string fileUrl)
{
bool result = false;//下载结果
WebResponse response = null;
try
{
WebRequest req = WebRequest.Create(fileUrl);
response = req.GetResponse();
result = response == null ? false : true;
}
catch (Exception ex)
{
result = false;
}
finally
{
if (response != null)
{
response.Close();
}
}
return result;
}
/// 通过传入的url获取远程文件数据(二进制流),大家可以通过Response.BinaryWrite()方法将获取的数据流输出
/// </summary>
/// <param name="url">图片的URL</param>
/// <param name="ProxyServer">代理服务器</param>
/// <returns>图片内容</returns>
public byte[] GetFile(string url, string proxyServer)
{
WebResponse rsp = null;
byte[] retBytes = null;
try
{
Uri uri = new Uri(url);
WebRequest req = WebRequest.Create(uri);
rsp = req.GetResponse();
Stream stream = rsp.GetResponseStream();
if (!string.IsNullOrEmpty(proxyServer))
{
req.Proxy = new WebProxy(proxyServer);
}
using (MemoryStream ms = new MemoryStream())
{
int b;
while ((b = stream.ReadByte()) != -1)
{
ms.WriteByte((byte)b);
}
retBytes = ms.ToArray();
}
}
catch (Exception ex)
{
retBytes = null;
}
finally
{
if (rsp != null)
{
rsp.Close();
}
}
return retBytes;
}
/// 将本地文件通过httpwebrequest上传到服务器
/// localFile:本地文件路径及文件名称,uploadUrl:远程路径(虚拟目录)
/// 在远程路径中需要把文件保存在的文件夹打开权限设置,否则上传会失败
/// <summary>
///
/// </summary>
/// <param name="localFile"></param>
/// <param name="uploadUrl"></param>
/// <returns></returns>
public bool UploadFileBinary(string localFile, string uploadUrl)
{
bool result = false;
HttpWebRequest req = null;
Stream reqStream = null;
FileStream rdr = null;
try
{
req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;
// Retrieve request stream
reqStream = req.GetRequestStream();
// Open the local file
rdr = new FileStream(localFile, FileMode.Open);
byte[] inData = new byte[4096];
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}
rdr.Close();
reqStream.Close();
req.GetResponse();
result = true;
}
catch (Exception e)
{
result = false;
}
finally
{
if (reqStream != null)
{
reqStream.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
return result;
}
30
2013
03
C# 判断远程图片和文件是否存在
发布:郑德才博客 | 分类:学习之路 | 评论:1 | 浏览:
相关文章:
C#、Asp.Net 对比两个实体信息前后是否有所改变(结构相同和不相同) (2016-9-2 20:24:29)
C#、Asp.Net 将一个实体对象转换为另一个实体对象(结构可以不一样) (2016-9-2 19:14:5)
【C#、Asp.Net 工具类大全】图片通用操作类 (2015-12-23 13:27:30)
【C#、Asp.Net 工具类大全】Request请求工具类 (2015-12-23 13:15:56)
【C#、Asp.Net 工具类大全】弹出提示操作类 (2015-12-23 13:6:1)
【C#、Asp.Net 工具类大全】正则匹配工具类 (2015-7-26 9:36:56)
【C#、Asp.Net 工具类大全】Js常用操作类 (2015-7-15 14:23:33)
【C#、Asp.Net 工具类大全】Html常用帮助类 (2015-7-11 23:42:53)
C#字符串编码帮助类 (2015-7-11 23:39:38)
【C#、Asp.Net 工具类大全】压缩文本、字节或者文件的压缩辅助类 (2015-7-11 23:23:49)
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
评论列表: