1. 程式人生 > >.net模擬http post請求上傳檔案

.net模擬http post請求上傳檔案

http://blog.csdn.net/dreamparks/article/details/43149845

public void SendByApi(string url, Stream postedStream, string fileName, string cType, Dictionary<string, string> formDataDic)

        {

            if (string.IsNullOrEmpty(url))

                throw new Exception("Upload Web URL Is Empty.");

            //時間戳 

            string strBoundary = "----" + DateTime.Now.Ticks.ToString("x");

            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");

            //請求頭部資訊 

            StringBuilder sb = new StringBuilder();

            sb.Append("--");

            sb.Append(strBoundary);

            sb.Append("\r\n");

            sb.Append("Content-Disposition: form-data; name=\"");

            sb.Append("file");

            sb.Append("\"; filename=\"");

            sb.Append(fileName);

            sb.Append("\"");

            sb.Append("\r\n");

            sb.Append("Content-Type: ");

            sb.Append(cType);

            sb.Append("\r\n");

            sb.Append("\r\n");

            string strPostHeader = sb.ToString();

            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            //Our method is post, otherwise the buffer (postvars) would be useless

            webRequest.Method = WebRequestMethods.Http.Post;

            // Proxy setting

            WebProxy proxy = new WebProxy();

            proxy.UseDefaultCredentials = true;

            webRequest.Proxy = proxy;

            //We use form contentType, for the postvars.

            webRequest.ContentType = "multipart/form-data;boundary=" + strBoundary; ;

            webRequest.ContentLength = postedStream.Length + postHeaderBytes.Length + boundaryBytes.Length;

            webRequest.Timeout = 300000;

            if (formDataDic != null)

            {

                foreach (string key in formDataDic.Keys)

                {

                    webRequest.Headers.Add(key, formDataDic[key]);

                }

            }

            byte[] fileByte = new byte[postedStream.Length];

            postedStream.Read(fileByte, 0, fileByte.Length);

            using (Stream requestStream = webRequest.GetRequestStream())

            {

                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                requestStream.Write(fileByte, 0, fileByte.Length);

                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

                requestStream.Close();

            }

            try

            {

                WebResponse resp = webRequest.GetResponse();

                Stream s = resp.GetResponseStream();

                StreamReader sr = new StreamReader(s);

                //讀取伺服器端返回的訊息 

                String sReturnString = sr.ReadLine();

            }

            catch (WebException ex)

            {

                HttpWebResponse response = ex.Response as HttpWebResponse;

                if (response != null)

                {

                    throw new WebException(response.StatusDescription, ex);

                }

                throw ex;

            }

            catch (Exception exception)

            {

                throw exception;

            }

        }

相關推薦

.net模擬http post請求檔案

http://blog.csdn.net/dreamparks/article/details/43149845 public void SendByApi(string url, Stream postedStream, string fileName, string

使用Python傳送http post請求檔案

可以使用第三方模組poster,使用easy_install 安裝方法如下 easy_instll poster 會自動安裝poster模組,安裝成功後,可以看到在poster egg檔案在site package下 這裡注意,根據官方說法:Because this dis

利用WebClient 模擬Form Post請求檔案

一.準備工作 1.開啟開發工具F12,檢視NetWork,首先先觀察目標網站上的請求頭 我們需要在程式碼中新增這些請求頭內容,比較重要的是Content-Type,User-Agent,當然可以全部新

post請求檔案和文字時http格式

服務端通常是根據請求頭(headers)中的 Content-Type 欄位來獲知請求中的訊息主體是用何種方式*編碼*,再對主體進行解析。所以說到 POST 提交資料方案,包含了 Content-Type 和訊息主體編碼方式兩部分。 application

Postman Post請求檔案

Postman Post請求上傳檔案 一、選擇post請求方式,輸入請求地址 二、填寫Headers Key:Content-Type Value:multipart/form-data [{"key":"Content-Type","value":"multipart/form-da

java 後臺通過post請求 檔案

文章轉載:https://blog.csdn.net/wohaqiyi/article/details/77621517   import microservice.fpzj.control.base.BaseController; import org.springframewo

關於post請求檔案和其它資料

1,背景: 在網上搜索這個問題得到的答案只有上傳檔案的單一上傳方式,並沒有說如果還需要其它請求引數的時候該如何處理。 2,解決方案: 通過append來加入其它請求引數,在上傳的時候還是用data:fromlsdata來上傳 3,相關程式碼: handleUp

python傳送post請求檔案,無法解析檔案

前言 近日,在做介面測試時遇到一個奇葩的問題。 使用post請求直接通過介面上傳檔案,無法識別檔案。 遇到的問題 以下是抓包得到的資訊: 以上請求是通過Postman直接傳送請求的。 在這裡可以看到訊息頭裡包含:Access-Token和Content-Type 因此在在使用python直接

通過POST請求檔案

轉自:https://blog.csdn.net/zhangge3663/article/details/81218488 理論 簡單的HTTP POST 大家通過HTTP向伺服器傳送POST請求提交資料,都是通過form表達提交的,程式碼如下:

使用HttpURLConnection的put或者post請求檔案

HttpURLConnection為java本身提供的傳送http請求的工具,今天遇到需要用put請求上傳檔案的需求,記錄下: 首先分析下put請求的格式,使用postman的put請求傳送一個檔案,由於現在有springboot,實現一個接受put請求傳送檔

使用HttpURlConnection 傳送POST請求檔案(帶引數)

前言 最近在做一個部落格的小專案,需要用到檔案上傳,HttpClient又被Android給棄用了,圖片框架暫時還沒學。只能使用HttpURLConnection來上傳。折騰了好久,今天終於順利地跟後臺完成了對接。因此,寫這篇部落格梳理一下知識。 理論知

Java利用HttpURLConnection傳送post請求檔案

在頁面裡實現上傳檔案不是什麼難事,寫個form,加上enctype = "multipart/form-data",在寫個接收的就可以了,沒什麼難的,如果要用java.net.HttpURLConnection來實現檔案上傳,還真有點搞頭.:-)   1.先寫個servle

HttpURLConnection Post請求檔案和引數到servlet

public String uplaod(String actionUrl, Map<String, String> params) {         InputStream in = null;         String BOUNDARY = java.util.UUID.randomUU

使用formidable獲取post請求檔案注意點

總結使用formidable曾經踩過的坑 遇到的問題都是因為使用post上傳檔案讀取不到的files 目前主要遇到的是以下兩種情況 沒有在表單<form></form>上新增enctype="multipart/form-data" 在有type

c# 模擬表單提交,post form 檔案、大資料內容

表單提交協議規定: 要先將 HTTP 要求的 Content-Type 設為 multipart/form-data,而且要設定一個 boundary 引數, 這個引數是由應用程式自行產生,它會用來識別每一份資料的邊界 (boundary), 用以產生多重資訊部份 (me

ASP.NET 利用post方式檔案

幾經努力,終於實現了檔案上傳,不使用asp.net元件,而使用傳統asp檔案模板方式,方法如下: 上傳檔案介面模板:(upfile.htm)HTML如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w

MFC通過Http請求檔案

FMC通過Http請求上傳檔案 void CMFCApplication1Dlg::HttpPostFile(string url, CString file, string paramName, string contentType) { CI

xUtils怎麽post請求json數據

測試 ces span 上傳 con utf-8 test exceptio cep InfoSmallCodeBinding smallCode = new InfoSmallCodeBinding(); smallCode.setSma

golang:模擬http post請求

brush else div str response inf 啟動 quest ack   1,發送http post請求(客戶端) func httppost() { data :=`{"type":"10","msg":"hello."}` request,

curl傳送請求檔案(multipart file upload)

折騰一下午的問題 第三方介面需要我們傳multipart 上傳檔案 curl一直各種試不成功,用Restlet Client工具總是能成功! 對比傳送的頭,發現工具在Content-Type: multipart/form-data;後面多了個這個boundary 然後去查了下