publicstaticintPostFile(stringgetUrl,CookieContainercookieContainer,HttpHeaderheader,stringpostdata,byte[]postdatabyte,Streamsm)
{
StreamfileStream;
if(sm!=null)
{
fileStream=sm;
}
else
{
fileStream=newMemoryStream(postdatabyte);
}
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12|SecurityProtocolType.Tls;//https形式需要增加
intreturnValue=0;
fileStream.Position=0;
varr=newBinaryReader(fileStream);
stringstrBoundary=”–“+DateTime.Now.Ticks.ToString(“x”);
byte[]boundaryBytes=Encoding.ASCII.GetBytes(“rn–“+strBoundary+”–rn”);
//恳求头部信息
StringBuildersb=newStringBuilder();
sb.Append(“–“);
sb.Append(strBoundary);
sb.Append(“rn”);
sb.Append(“Content-Disposition:form-data;name=”importType””);
sb.Append(“rnrn1”);
sb.Append(“rn”);
sb.Append(“–“);
sb.Append(strBoundary);
sb.Append(“rn”);
sb.Append(“Content-Disposition:form-data;name=”file”;filename=”1.xls””);
sb.Append(“rn”);
sb.Append(“Content-Type:”);
sb.Append(“application/octet-stream”);
sb.Append(“rnrn”);
stringstrPostHeader=sb.ToString();
byte[]postHeaderBytes=Encoding.UTF8.GetBytes(strPostHeader);
try
{
//依据uri创建HttpWebRequest对象
HttpWebRequesthttpReq=(HttpWebRequest)WebRequest.Create(newUri(getUrl));
httpReq.Method=”POST”;
//对发送的数据不使用缓存
httpReq.AllowWriteStreamBuffering=false;
//设置取得呼应的超时时刻(300秒)
httpReq.Timeout=300000;
//httpReq.ServicePoint.Expect100Continue=false;
httpReq.CookieContainer=cookieContainer;
httpReq.Accept=header.accept;
//httpReq.Headers.Add(“Accept-Encoding”,”gzip,deflate,br”);
//httpReq.Headers.Add(“TE”,”Trailers”);
httpReq.UserAgent=header.userAgent;
httpReq.ContentType=”multipart/form-data;boundary=”+strBoundary;
longlength=fileStream.Length+postHeaderBytes.Length+boundaryBytes.Length;
longfileLength=fileStream.Length;
httpReq.ContentLength=length;
byte[]buffer=newbyte[fileLength];
StreampostStream=httpReq.GetRequestStream();
//发送恳求头部音讯
postStream.Write(postHeaderBytes,0,postHeaderBytes.Length);
intsize=r.Read(buffer,0,buffer.Length);
postStream.Write(buffer,0,size);
//增加尾部的时刻戳
postStream.Write(boundaryBytes,0,boundaryBytes.Length);
postStream.Close();
//获取服务器端的呼应
HttpWebResponsewebRespon=(HttpWebResponse)httpReq.GetResponse();
if(webRespon.StatusCode==HttpStatusCode.OK)//如果服务器未呼应,那么继续等候相应
{
Streams=webRespon.GetResponseStream();
StreamReadersr=newStreamReader(s);
//读取服务器端返回的音讯
StringsReturnString=sr.ReadLine();
s.Close();
sr.Close();
fileStream.Close();
returnValue=returnValue;
}
}
catch(Exceptionex)
{
UnionLog.WriteLog(LogType.UNION_ERROR,string.Format(“导入Excel失利:{0}”,ex.Message));
}
returnreturnValue;
}
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com