当前位置:首页 » 文件管理 » httpwebrequest上传文件

httpwebrequest上传文件

发布时间: 2022-08-11 09:14:53

❶ c#编程中文件的上传和下载要求断点上传断点下载的详细代码急需!!是C#的。。

C#断点续传下载核心代码

static void Main(string[] args)
{

string StrFileName = "c:\\aa.zip"; //根据实际情况设置
string StrUrl = "http://www.xxxx.cn/xxxxx.zip"; //根据实际情况设置

//打开上次下载的文件或新建文件
long lStartPos = 0;
System.IO.FileStream fs;
if (System.IO.File.Exists(StrFileName))
{
fs = System.IO.File.OpenWrite(StrFileName);
lStartPos = fs.Length;
fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fs = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
lStartPos = 0;
}

//打开网络连接
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(StrUrl);
if (lStartPos > 0)
request.AddRange((int) lStartPos); //设置Range值

//向服务器请求,获得服务器回应数据流
System.IO.Stream ns = request.GetResponse().GetResponseStream();

byte[] nbytes = new byte[512];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, 512);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, 512);
}
fs.Close();
ns.Close();
Console.WriteLine("下载完成");
}
catch (Exception ex)
{
fs.Close();
Console.WriteLine("下载过程中出现错误:" + ex.ToString());
}
}

❷ HttpWebRequest和HttpWebResponse消息的上传和反馈信息的处理

你的代码,第一段图片代码,与第二段手写代码,是不是一个意思?

你的代码,可以满足你的要求。

❸ C# HttpWebRequest上传大文件(200M)提示远程主机强迫关闭了一个现有的连接

服务器是自己的? 很多服务器本身有限制上传时间,可以不限制大小,但会限制上传时间。

❹ 如何用httpRequest,WebRequest等类获取网页文件信息并且下载文件呢

首先 得购买空间:服务商会给你一个FTP地址和用户名 密码
其次:找一个FTP软件 flashfxp 输入服务商提供的FTP地址 用户名 密码登陆 上传

或直接打开浏览器 输入FTP地址 输入用户名 密码 把做好的网页 复制 粘贴上去主OK了

❺ C# 判断图片链接是否存在

/************************
*用第二个方法,获取远程文件的大小
*************************/
//1.判断远程文件是否存在

///fileUrl:远程文件路径,包括IP地址以及详细的路径

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;
}

/// 2.通过传入的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();
}

///将本地文件通过httpwebrequest上传到服务器

///localFile:本地文件路径及文件名称,uploadUrl:远程路径(虚拟目录)

///在远程路径中需要把文件保存在的文件夹打开权限设置,否则上传会失败

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;
}

}
return retBytes;
}

❻ 谁知道C#使用HttpWebRequest的POST方法发送大量数据的方法

不想一点一点写了,粘贴给你吧

使用 HttpWebRequest 向网站提交数据
HttpWebRequest 是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过 HTTP 协议和服务器交互。

HttpWebRequest 对 HTTP 协议进行了完整的封装,对 HTTP 协议中的 Header, Content, Cookie 都做了属性和方法的支持,很容易就能编写出一个模拟浏览器自动登录的程序。

程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成,下面对这两种方式进行一下说明:

1. GET 方式。 GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 中,前面部分 表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。程序代码如下:

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

2. POST 方式。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。程序代码如下:

string param = "hl=zh-CN&newwindow=1";
byte[] bs = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

在上面的代码中,我们访问了 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。

3. 使用 GET 方式提交中文数据。 GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种,用 gb2312 方式编码访问的程序代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string address = "" + HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

在上面的程序代码中,我们以 GET 方式访问了网址 ,传递了参数“参数一=值一”,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。常见的网站中, (网络)的编码方式是 gb2312, (谷歌)的编码方式是 utf8。

4. 使用 POST 方式提交中文数据。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,由于提交的参数中可以说明使用的编码方式,所以理论上能获得更大的兼容性。用 gb2312 方式编码访问的程序代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding) + "&" + HttpUtility.UrlEncode("参数二", myEncoding) + "=" + HttpUtility.UrlEncode("值二", myEncoding);

byte[] postBytes = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;

using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

从上面的代码可以看出, POST 中文数据的时候,先使用 UrlEncode 方法将中文字符转换为编码后的 ASCII 码,然后提交到服务器,提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。

以上列出了客户端程序使用 HTTP 协议与服务器交互的情况,常用的是 GET 和 POST 方式。现在流行的 WebService 也是通过 HTTP 协议来交互的,使用的是 POST 方法。与以上稍有所不同的是, WebService 提交的数据内容和接收到的数据内容都是使用了 XML 方式编码。所以, HttpWebRequest 也可以使用在调用 WebService 的情况下。

❼ C#用HttpWebRequest上传数据的问题。

Request可以当成全局变量同url一起传进来
在方法里只修改Reqest的访问地址就可以了
也就是不用没次都要给Request分配内存 你不是要循环的吗
其它的都没什么问题

❽ C#HttpWebRequest使用

CookieContainer cc = new CookieContainer();
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = string.Empty;
postData = "username=" + username;//比如网页上输入框的名字为uname
postData += "&password=" + pwd;
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("网址");//登陆页地址
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = cc;
myRequest.KeepAlive = true;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //接收返回值
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();

❾ httpWebRequest.GetResponse() 基础连接已关闭 发送时发生错误

这个不是超时问题,在XP下只要超过125M以上一提交接就报错,WIN7下只要服务器支持超时时间范围内,就算1G的文件传一天也不会超时,因为.NET框架底层的WebRequest是用IE的wininet.dll来进行网络请求的,所以跟不同系统环境下的IE内核有关,要解决此问题只有一个办法,就是自己用SOCKET来进行HTTP请求

❿ wince如何实现上传图片到web服务器,我使用的是HttpWebRequest急求

private static void UploadFile(Stream postedStream, string fileName,
2 string uploadURL, string fileGroup, string fileType,
3 string specialPath, string userName, string uploadType)
4 {
5 if (string.IsNullOrEmpty(uploadURL))
6 throw new Exception("Upload Web URL Is Empty.");
7
8 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uploadURL);
9
10 //Our method is post, otherwise the buffer (postvars) would be useless
11 webRequest.Method = WebRequestMethods.Http.Post;
12
13 // Proxy setting
14 WebProxy proxy = new WebProxy();
15 proxy.UseDefaultCredentials = true;
16 webRequest.Proxy = proxy;
17
18 //We use form contentType, for the postvars.
19 webRequest.ContentType = "application/x-www-form-urlencoded";
20
21 webRequest.Headers.Add("FileGroup", fileGroup);
22 webRequest.Headers.Add("FileType", fileType);
23 webRequest.Headers.Add("UploadType", uploadType);
24 webRequest.Headers.Add("FileName", fileName);
25 webRequest.Headers.Add("UserName", userName);
26 webRequest.Headers.Add("SpecialFolderPath", specialPath);
27
28 using (Stream requestStream = webRequest.GetRequestStream())
29 {
30 FileUtility.CopyStream(postedStream, requestStream);
31 }
32 try
33 {
34 webRequest.GetResponse().Close();
35 }
36 catch (WebException ex)
37 {
38 HttpWebResponse response = ex.Response as HttpWebResponse;
39 if (response != null)
40 {
41 throw new WebException(response.StatusDescription, ex);
42 }
43
44 throw ex;
45 }
46 catch (Exception exception)
47 {
48 throw exception;
49 }
50 }

热点内容
app什么情况下找不到服务器 发布:2025-05-12 15:46:25 浏览:714
php跳过if 发布:2025-05-12 15:34:29 浏览:467
不定时算法 发布:2025-05-12 15:30:16 浏览:131
c语言延时1ms程序 发布:2025-05-12 15:01:30 浏览:166
动物园灵长类动物配置什么植物 发布:2025-05-12 14:49:59 浏览:736
wifi密码设置什么好 发布:2025-05-12 14:49:17 浏览:148
三位数乘两位数速算法 发布:2025-05-12 13:05:48 浏览:397
暴风影音缓存在哪里 发布:2025-05-12 12:42:03 浏览:542
access数据库exe 发布:2025-05-12 12:39:04 浏览:630
五开的配置是什么 发布:2025-05-12 12:36:37 浏览:365