Новые сообщения [новые:0]
Дайджест
Горячие темы
Избранное [новые:0]
Форумы
Пользователи
Статистика
Статистика нагрузки
Мод. лог
Поиск
|
27.02.2013, 16:23
|
|||
---|---|---|---|
HTTPWebrequest post загрузка файла |
|||
#18+
Добрый день. Неоходимо загрузить файл на сервер. Использую следующий код: private static string UploadFilesToRemoteUrl(string url, string file, NameValueCollection nvc) { string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary; httpWebRequest.Method = "POST"; httpWebRequest.KeepAlive = true; byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n"; string header = string.Format(headerTemplate, "file", file, "application/binary"); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); httpWebRequest.ContentLength = new FileInfo(file).Length + headerbytes.Length + (boundarybytes.Length * 2) + 2; Stream requestStream = httpWebRequest.GetRequestStream(); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in nvc.Keys) { requestStream.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, nvc[key]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); requestStream.Write(formitembytes, 0, formitembytes.Length); } requestStream.Write(boundarybytes, 0, boundarybytes.Length); requestStream.Write(boundarybytes, 0, boundarybytes.Length); requestStream.Write(headerbytes, 0, headerbytes.Length); FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); requestStream.Flush(); } boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(boundarybytes, 0, boundarybytes.Length); requestStream.Close(); fileStream.Close(); WebResponse webResponse = httpWebRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); StreamReader responseReader = new StreamReader(responseStream); string responseString = responseReader.ReadToEnd(); webResponse.Close(); return responseString; } После вызова метода получаю ошибку - Bytes to be written to the stream exceed the Content-Length bytes size specified. В чем причина? ... |
|||
:
Нравится:
Не нравится:
|
|||
|
27.02.2013, 16:28
|
|||
---|---|---|---|
HTTPWebrequest post загрузка файла |
|||
#18+
ContentLength посчитал не правильно, что же еще. Ее вообще считать не надо, HttpWebRequest сам почитает длину того, что в него понапихали, и создаст заголовок. ... |
|||
:
Нравится:
Не нравится:
|
|||
|
27.02.2013, 16:30
|
|||
---|---|---|---|
HTTPWebrequest post загрузка файла |
|||
#18+
Antonariy, спасибо ... |
|||
:
Нравится:
Не нравится:
|
|||
|
27.02.2013, 16:36
|
|||
---|---|---|---|
HTTPWebrequest post загрузка файла |
|||
#18+
ильдар4, При загрузке больших файлов выдает ошибку Timeout exception ... |
|||
:
Нравится:
Не нравится:
|
|||
|
27.02.2013, 16:55
|
|||
---|---|---|---|
HTTPWebrequest post загрузка файла |
|||
#18+
ну не грузи большие файлы, чо или увеличь таймаут на сервере, если ты им управляешь ... |
|||
:
Нравится:
Не нравится:
|
|||
|
27.02.2013, 16:59
|
|||
---|---|---|---|
HTTPWebrequest post загрузка файла |
|||
#18+
Antonariy, Необходимо грузить большие файлы ... |
|||
:
Нравится:
Не нравится:
|
|||
|
|
start [/forum/topic.php?fid=20&tablet=1&tid=1405093]: |
0ms |
get settings: |
7ms |
get forum list: |
12ms |
check forum access: |
3ms |
check topic access: |
3ms |
track hit: |
49ms |
get topic data: |
11ms |
get forum data: |
2ms |
get page messages: |
44ms |
get tp. blocked users: |
1ms |
others: | 15ms |
total: | 147ms |
0 / 0 |