日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 使用HttpWebRequest通过PHP接口 上传文件

發(fā)布時(shí)間:2024/10/12 C# 106 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 使用HttpWebRequest通过PHP接口 上传文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

1:上傳文件實(shí)例

?public void UploadXMLLog(string xmlpath) ??????? { ??????????? NameValueCollection nvc = new NameValueCollection(); ??????????? CookieContainer cookies = new CookieContainer(); ??????????? nvc.Add("", “”); ????????????...... ??????????? string url = "UrlPath"; ??????????? string res = UploadFile(xmlpath, url, nvc, cookies); ??????? }

2:UploadFile源碼

1 public string UploadFile(string uploadfile, string url, NameValueCollection querystring, CookieContainer cookies, string fileFormName = "file", string contenttype = "multipart/form-data") 2 { 3 if ((fileFormName == null) || 4 (fileFormName.Length == 0)) 5 { 6 fileFormName = "file"; 7 } 8 9 if ((contenttype == null) || 10 (contenttype.Length == 0)) 11 { 12 contenttype = "application/octet-stream"; 13 } 14 Uri uri = new Uri(url); 15 string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); 16 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); 17 webrequest.CookieContainer = cookies; 18 webrequest.ContentType = "multipart/form-data; boundary=" + boundary; 19 webrequest.Method = "POST"; 20 StringBuilder sb = new StringBuilder(); 21 sb.Append("--"); 22 sb.Append(boundary); 23 sb.Append("\r\n"); 24 sb.Append("Content-Disposition: form-data; name=\""); 25 sb.Append(fileFormName); 26 sb.Append("\"; filename=\""); 27 sb.Append(uploadfile); 28 sb.Append("\""); 29 sb.Append("\r\n"); 30 sb.Append("Content-Type: "); 31 sb.Append(contenttype); 32 sb.Append("\r\n"); 33 sb.Append("\r\n"); 34 35 string postHeader = sb.ToString(); 36 byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); 37 byte[] boundaryBytes = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); 38 byte[] br = Encoding.ASCII.GetBytes("\r\n"); 39 FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); 40 long length = postHeaderBytes.Length + fileStream.Length + br.Length; 41 if (querystring != null) 42 { 43 44 StringBuilder sub = new StringBuilder(); 45 foreach (string key in querystring.Keys) 46 { 47 sub.Append("--"); 48 sub.Append(boundary); 49 sub.Append("\r\n"); 50 sub.Append("Content-Disposition: form-data; name=\""); 51 sub.Append(key); 52 sub.Append("\""); 53 sub.Append("\r\n"); 54 sub.Append("\r\n"); 55 sub.Append(querystring[key]); 56 sub.Append("\r\n"); 57 byte[] formitembytes = Encoding.UTF8.GetBytes(sub.ToString()); 58 length += formitembytes.Length; 59 } 60 } 61 length += boundaryBytes.Length; 62 webrequest.ContentLength = length; 63 Stream requestStream = webrequest.GetRequestStream(); 64 // Write out our post header 65 requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); 66 67 // Write out the file contents 68

轉(zhuǎn)載于:https://www.cnblogs.com/chun6/p/6230868.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的C# 使用HttpWebRequest通过PHP接口 上传文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。