SendingFile+ParametersinPostRequest我正在使用这段代码向网页发送参数并从中获得正确的响应。System.Net.WebClientoWeb=newSystem.Net.WebClient();oWeb.Proxy=System.Net.WebRequest.DefaultWebProxy;oWeb.Proxy.Credentials=System.Net.CredentialCache.DefaultCredentials;oWeb.Headers.Add("Content-Type","application/x-www-form-urlencoded");byte[]bytArguments=System.Text.Encoding.ASCII.GetBytes("value1=123&value2=xyz");byte[]bytRetData=oWeb.UploadData("http://website.com/file.php","POST",bytArguments);response=System.Text.Encoding.ASCII.GetString(bytRetData);但是现在我想发送一个像(.doc)这样的文件给它+上面的参数(value1,value2),但我不知道该怎么做。使用WebClient.QueryString传递与请求关联的名称/值对。NameValueCollection参数=newNameValueCollection();parameters.Add("value1","123");parameters.Add("value2","xyz");oWeb.QueryString=参数;varresponseBytes=oWeb.UploadFile("http://website.com/file.php","文件路径");字符串响应=Encoding.ASCII.GetString(responseBytes);publicstaticvoidHttpUploadFile(stringurl,stringfile,stringparamName,stringcontentType,NameValueCollectionnvc){stringboundary="--------------------------"+DateTime.Now.Ticks.ToString("x");byte[]boundarybytes=System.Text.Encoding.ASCII.GetBytes("rn--"+boundary+"rn");HttpWebRequestwr=(HttpWebRequest)WebRequest.Create(url);wr.ContentType="multipart/form-data;boundary="+boundary;wr.Method="POST";wr.KeepAlive=true;wr.Credentials=System.Net.CredentialCache.DefaultCredentials;流rs=wr.GetRequestStream();stringformdataTemplate="Content-Disposition:form-data;name="{0}"rnrn{1}";foreach(字符串键innvc.Keys){rs.Write(boundarybytes,0,boundarybytes.Length);stringformitem=string.Format(formdataTemplate,key,nvc[key]);byte[]formitembytes=System.Text.Encoding.UTF8.GetBytes(formitem);rs.Write(formitembytes,0,formitembytes.Length);}rs.Write(boundarybytes,0,boundarybytes.Length);stringheaderTemplate="Content-Disposition:form-data;name="{0}";filename="{1}"rnContent-Type:{2}rnrn";stringheader=string.Format(headerTemplate,paramName,file,contentType);byte[]headerbytes=System.Text.Encoding.UTF8.GetBytes(header);rs.Write(headerbytes,0,headerbytes.Length);FileStreamfileStream=newFileStream(文件,FileMode.Open,FileAccess.Read);字节[]缓冲区=新字节[4096];intbytesRead=0;while((bytesRead=fileStream.Read(buffer,0,buffer.Length))!=0){rs.Write(buffer,0,bytesRead);}fileStream.Close();byte[]trailer=System.Text.Encoding.ASCII.GetBytes("rn--"+boundary+"--rn");rs.Write(预告片,0,尾迹呃.长度);rs.关闭();WebResponsewresp=null;尝试{wresp=wr.GetResponse();流stream2=wresp.GetResponseStream();StreamReaderreader2=newStreamReader(stream2);结果=reader2.ReadToEnd();}catch(Exceptionex){System.Windows.MessageBox.Show("转换文件时出错","Error!");if(wresp!=null){wresp.Close();wresp=null;}}最后{wr=null;从SO复制但不记得它的链接这是它的使用方式NameValueCollectionnvc=newNameValueCollection();nvc.Add("parm1","value1");nvc.Add("parm2","value2");nvc.Add("parm3","value3");HttpUploadFile("http://www.example.com/upload.php",@filepath,"file","text/html",nvc);这里@filepath是你要上传的文件的路径:c:file_to_upload.docfile是php中使用的字段名$_Files['file']以上是C#学习教程:sendinpostrequestAll文件+参数分享的内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
