您的位置:首页 > 理论基础 > 计算机网络

C#如何用WebClient动态提交文件至Web服务器和设定Http响应超时时间

2010-07-02 09:51 696 查看
WebClient方法本身没有提供设定http请求响应超时时间的方法,我们需要重写该该类的GetWebRequest
方法,代码如下。

类名为 CGMWebClient.cs
可以根据自己的要求修改

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace cvsFileUploadCommSub
{
public class CGMWebClient : System.Net.WebClient
{

private int _timeOut = 100;
/**/
/// <summary>
/// 过期时间 单位秒
/// </summary>
public int Timeout
{
get
{
return _timeOut;
}
set
{
if (value <= 0)
_timeOut = 100;
_timeOut = value;
}
}
/**/
/// <summary>
/// 重写GetWebRequest,添加WebRequest对象超时时间
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.Timeout = 1000 * Timeout;
request.ReadWriteTimeout = 1000 * Timeout;
return request;
}

}
}


C/S端用户提交数据的测试方法方法1:
(里面有几个类需要引入,自己引入一下就可以了


/// <summary>
/// 上传字符长到指定服务器测试函数
/// </summary>
/// <returns></returns>
public static string UpdataStringTest()
{
//提交已知文本
//服务器上传Url地址(需要改成你自己本地的服务器地址,具体代码会在下面给出)
string strUrl = "http://www.my400800.cn";
string strRet = "";//返回值
string strFileName = "20100702_0000_0059.csv";//服务器端保存用文件名
string strUploadCvsText = @"百度,http://www.baidu.com,192.168.0.12,2010-07-02 08:00:01
google,http://www.google.com.hk,192.168.0.20,2010-07-02 08:00:01
400电话,http://www.my400800.cn,192.168.0.11,2010-07-02 08:00:10
电话,http://www.tel4006.com,192.168.0.122010-07-02 08:00:12
中国移动,http://www.chinamobile.com,192.168.0.23,2010-07-02 08:00:30
淘宝,http://www.taobao.com,192.168.0.81,2010-07-02 08:00:60"; //需要上传的文本内容
CGMWebClient wc = new CGMWebClient();
wc.Timeout = 10 * 60;//10分钟
//用默认编码转换网页内容
Encoding encoding = Encoding.GetEncoding("utf-8");

if (strUploadCvsText.Length > 10)//文件长度小于10时不上传文件
{
byte[] buffer = wc.UploadData(strUrl + "?fileName=" + strFileName, encoding.GetBytes(strUploadCvsText));
strRet = encoding.GetString(buffer);
strRet = strRet.Replace(" ", "").Replace("/r/n", "");
}
else
{
strRet = "【" + strFileName + "】没有可上传数据。";
}
return strRet;
}


C/S端用户提交数据的测试方法方法2:
(里面有几个类需要引入,自己引入一下就可以了


/// <summary>
/// 上传指定文件到Web服务器测试函数
/// </summary>
/// <returns></returns>
public static string UpdataFileWebServerTest()
{
//提交已知文本
//服务器上传Url地址(需要改成你自己本地的服务器地址,具体代码会在下面给出)
string strUrl = "http://www.my400800.cn";
string strRet = "";//返回值
string strFileName = "20100702_0000_0059.rar";//服务器端保存用文件名
string strUpFilePath = "E:/test.rar";//你本地文件存放路径,需要根据自己的需求修改
CGMWebClient wc = new CGMWebClient();
wc.Timeout = 10 * 60;//10分钟
if (!System.IO.File.Exists(strUpFilePath))//文件长度小于10时不上传文件
{
//用默认编码转换网页内容
Encoding encoding = Encoding.GetEncoding("utf-8");
//文件读取
byte[] readFileByteArr = System.IO.File.ReadAllBytes("strUpFilePath");
byte[] buffer = wc.UploadData(strUrl + "?fileName=" + strFileName, readFileByteArr);
strRet = encoding.GetString(buffer);
strRet = strRet.Replace(" ", "").Replace("/r/n", "");
}
else
{
strRet = "【" + strFileName + "】没有可上传数据。";
}
return strRet;
}


web端代码(iis6.0,asp.net 2.0)

CsvDataUpload.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class CsvDataUpload : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
string strResponse = "";
try
{
strResponse = UpFileData();
}
catch (Exception ex)
{
strResponse = ex.ToString();
}

Response.Write(strResponse);
// Response.Write(UPdataData());
}

/// <summary>
/// 文件上传处理
/// </summary>
/// <returns></returns>
public string UpFileData()
{
string strRet = "";
if (Request.InputStream.Length == 0)
{
//没有内容
strRet = "没有上传文件,文件长度是0。";
return strRet;
}
string strFileName = Convert.ToString(Request["fileName"]);
if (string.IsNullOrEmpty(strFileName))
{
strRet = "文件名称错误,文件上传失败。";
return strRet;
}
string strRoot = BaseComm.CommSub.GetServerPath() + "//Updata//";
if (!System.IO.Directory.Exists(strRoot))
{
System.IO.Directory.CreateDirectory(strRoot);
}
strRoot += strFileName.Substring(0, 8) + "//";
if (!System.IO.Directory.Exists(strRoot))
{
System.IO.Directory.CreateDirectory(strRoot);
}
strRoot += strFileName;
if (System.IO.File.Exists(strRoot))
{
System.IO.File.Delete(strRoot);
}
System.IO.File.WriteAllBytes(strRoot, StreamToBytes(Request.InputStream));
return "文件【" + strFileName + "】上传完成。";

}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
}


CsvDataUpload.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CsvDataUpload.aspx.cs" Inherits="CsvDataUpload" %>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐