當前位置:首頁 » 文件管理 » 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 瀏覽:165
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:734
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:148
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:397
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:541
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:628
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:365