欢迎访问 生活随笔!

ag凯发k8国际

当前位置: ag凯发k8国际 > 编程语言 > c# >内容正文

c#

c# 使用httpwebrequest通过php接口 上传文件 -ag凯发k8国际

发布时间:2024/10/12 c# 26 豆豆
ag凯发k8国际 收集整理的这篇文章主要介绍了 c# 使用httpwebrequest通过php接口 上传文件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

 

1:上传文件实例

 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

转载于:https://www.cnblogs.com/chun6/p/6230868.html

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是ag凯发k8国际为你收集整理的c# 使用httpwebrequest通过php接口 上传文件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得ag凯发k8国际网站内容还不错,欢迎将ag凯发k8国际推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图