问题描述
C#语言winform做模拟登陆到微信公众平台进行图片上传,图片上传返回的是{"base_resp":{"ret":-2,"err_msg":"invalidargs"}}上传失败,微信公众平台获取如下:RemoteAddress:101.226.68.61:443RequestURL:https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&ticket_id=NicSnow&ticket=5c797e3a8d6db1b5af81078f99e77a97467c7fd7&token=1263014585&lang=zh_CNRequestMethod:POSTStatusCode:200OKRequestHeadersviewsourceAccept:*/*Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8Connection:keep-aliveContent-Length:3332Content-Type:multipart/form-data;boundary=----------Ef1ae0ei4gL6gL6ae0GI3gL6KM7ae0Cookie:remember_acct=626617118%40qq.com;_gscu_661903259=03504420e7ugzy19;piao_city=125;pgv_pvi=1019938816;RK=xZ1vcoKQGo;ts_refer=www.baidu.com/s;ts_uid=8409079640;lv_irt_id=e9a87a2c315e80da14e4ccac5f33ca3a;ptui_loginuin=1139232377;qqB_short=1;isVideo_DC=0;pgv_info=ssid=s2537644878;pgv_pvid=8069529924;o_cookie=626617118;pt2gguin=o0626617118;uin=o0626617118;skey=@zXGxVBSM9;ptisp=ctc;ptcz=712ee3482f231aae599407a5cf37d4aecafb443808a38ce7cf5a0635be3047a3;data_bizuin=2398190748;data_ticket=AgU54xCIhpkULXiAfcqE/uXt;remember_acct=626617118%40qq.com;slave_user=gh_d40b9f0c3bd9;slave_sid=MlpkdlllemVFaFhUcnFDZ3FXUlBacWgwdFhTV0lqZlVISlhOY2FWQ2lnVHBDbW1qS25xblZVTnBybXN1TGtZN2I2bmVIWmtfVHg5RDRjODVrOVl0WHM2SzVNYU5aWkNEekxiUnAxT2VIQ1kwV29ZZkRUbnlzdTdpK2w2T2xGM2w=;bizuin=3097552336Host:mp.weixin.qq.comOrigin:https://mp.weixin.qq.comReferer:https://mp.weixin.qq.com/cgi-bin/filepage?type=2&begin=0&count=20&t=media/list&token=1263014585&lang=zh_CNUser-Agent:Mozilla/5.0(WindowsNT6.2;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/36.0.1985.143Safari/537.36QueryStringParametersviewparsedaction=upload_material&f=json&ticket_id=NicSnow&ticket=5c797e3a8d6db1b5af81078f99e77a97467c7fd7&token=1263014585&lang=zh_CNRequestPayload------------Ef1ae0ei4gL6gL6ae0GI3gL6KM7ae0Content-Disposition:form-data;name="Filename"no.gif------------Ef1ae0ei4gL6gL6ae0GI3gL6KM7ae0Content-Disposition:form-data;name="folder"/cgi-bin/uploads------------Ef1ae0ei4gL6gL6ae0GI3gL6KM7ae0Content-Disposition:form-data;name="file";filename="no.gif"Content-Type:application/octet-stream------------Ef1ae0ei4gL6gL6ae0GI3gL6KM7ae0Content-Disposition:form-data;name="Upload"SubmitQuery------------Ef1ae0ei4gL6gL6ae0GI3gL6KM7ae0--ResponseHeadersviewsourceContent-Length:96Content-Type:application/json;charset=UTF-8代码如下:stringurl="https://mp.weixin.qq.com/cgi-bin/filetransfer";stringparam="action=upload_material&f=json&ticket_id=NicSnow&ticket=f438982cc1892b478d1d64275db91d106190a05b&token="+WeiXinLogin.LoginInfo.Token+"&lang=zh_CN";stringfileformname="file";stringfilepath=@"F:no.gif";stringfilename="no.gif";stringrefre="https://mp.weixin.qq.com/cgi-bin/filepage?type=2&begin=0&count=20&t=media/list&token="+WeiXinLogin.LoginInfo.Token+"&lang=zh_CN";stringboundary="----------Zf1ae0ei4gL6gL6ae0GI3gL6KM7ae0";HttpWebRequestwebrequest=(HttpWebRequest)WebRequest.Create(url);webrequest.ContentType="multipart/form-data;boundary="+boundary;webrequest.Method="POST";//webrequest.Headers.Add("Cookie:"+WeiXinLogin.LoginInfo.LoginCookie);webrequest.CookieContainer=WeiXinLogin.LoginInfo.LoginCookie;webrequest.Referer=refre;FileStreamfileStream=newFileStream(filepath,FileMode.Open,FileAccess.Read);byte[]paramBytes=Encoding.UTF8.GetBytes(param);StringBuildersb=newStringBuilder();sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="Filename"rn");sb.Append("rn");sb.Append("no.gifrn");sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="folder"rn");sb.Append("rn");sb.Append("/cgi-bin/uploadsrn");sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="file";filename="no.gif"rn");sb.Append("Content-Type:application/octet-streamrn");sb.Append("rn");sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="Upload"rn");sb.Append("rn");sb.Append("SubmitQueryrn");sb.Append("--"+boundary+"--");stringrequestpayload=sb.ToString();byte[]payloadBytes=Encoding.UTF8.GetBytes(requestpayload);byte[]buffer=newByte[checked((uint)Math.Min(4096,(int)fileStream.Length))];webrequest.ContentLength=buffer.Length+paramBytes.Length+payloadBytes.Length;StreamnewStream=webrequest.GetRequestStream();//intbytesRead=0;//while((bytesRead=fileStream.Read(buffer,0,buffer.Length))!=0)newStream.Write(paramBytes,0,paramBytes.Length);newStream.Write(payloadBytes,0,payloadBytes.Length);newStream.Write(buffer,0,buffer.Length);newStream.Close();HttpWebResponseresponse2=(HttpWebResponse)webrequest.GetResponse();StreamReadersr2=newStreamReader(response2.GetResponseStream(),Encoding.Default);returnsr2.ReadToEnd();
解决方案
解决方案二:
把你的这段代码FileStreamfileStream=newFileStream(filepath,FileMode.Open,FileAccess.Read);byte[]paramBytes=Encoding.UTF8.GetBytes(param);StringBuildersb=newStringBuilder();sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="Filename"rn");sb.Append("rn");sb.Append("no.gifrn");sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="folder"rn");sb.Append("rn");sb.Append("/cgi-bin/uploadsrn");sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="file";filename="no.gif"rn");sb.Append("Content-Type:application/octet-streamrn");sb.Append("rn");sb.Append("--"+boundary+"rn");sb.Append("Content-Disposition:form-data;name="Upload"rn");sb.Append("rn");sb.Append("SubmitQueryrn");sb.Append("--"+boundary+"--");stringrequestpayload=sb.ToString();byte[]payloadBytes=Encoding.UTF8.GetBytes(requestpayload);byte[]buffer=newByte[checked((uint)Math.Min(4096,(int)fileStream.Length))];webrequest.ContentLength=buffer.Length+paramBytes.Length+payloadBytes.Length;StreamnewStream=webrequest.GetRequestStream();//intbytesRead=0;//while((bytesRead=fileStream.Read(buffer,0,buffer.Length))!=0)newStream.Write(paramBytes,0,paramBytes.Length);newStream.Write(payloadBytes,0,payloadBytes.Length);newStream.Write(buffer,0,buffer.Length);newStream.Close();
改成下面的代码FileStreamfileStream=newFileStream(filepath,FileMode.Open,FileAccess.Read);byte[]buffer=newbyte[fileStream.Length];fileStream.Read(buffer,0,buffer.Length);fileStream.Close();webrequest.ContentLength=buffer.Length;StreamnewStream=webrequest.GetRequestStream();newStream.Write(buffer,0,buffer.Length);newStream.Close();
解决方案三:
@sunny906改了但是还是一样参数无效