关于HttpWebRequest的一些棘手问题!!!
最近在做一个C/S架构的的项目,项目是基于模拟登陆到阿里巴巴国际站进行产品的批量发布,之前是做了一套java版的b/s架构的,已经完成。c/s版的采用c#winform 做,由于本人是做java的,对c#不太精通,在做图片上传的时候遇到个文件解决不了,求.net高人指点:在使用HttpWebRequest模拟浏览器post请求上传大图片的时候不成功,一百kb左右的图片是可以正常上传的,阿里的规定是3MB以内可以上传,在使用fiddler监控的时候发现我的程序上传的请求如下图
Content-Length: 2700661
而使用阿里网站的上传入口发现
阿里巴巴的Content-Length明细比我的Content-Length小了许多(上传的图片文件是同一张),我猜测是因为我没有对请求流没有进行压缩导致Content-Length太大了,所以上传失败。问题是我写的java版的不存在这个问题。大图也能上传成功,而c#版上传失败。郁闷。。。 --------------------编程问答-------------------- stream stm=httpwebrequest.getrequeststream()
stm.write(bt,0,bt.length);
stm.disponse();
不可能有问题的,这玩意我上传几十兆都没问题,怀疑你没用disponse或者flush --------------------编程问答--------------------
你是不是调用了WCF或WebService的maxReceivedMessageSize设置到最大2147483647,即
maxReceivedMessageSize="2147483647"
有可能是缓冲大小不够. --------------------编程问答-------------------- 一个png一个jpg,好歹你得用一个文件试。 --------------------编程问答--------------------
public static string createFilePost(string file,string url, ref CookieCollection cookies)
{
//返回内容
string httpWebContent = string.Empty;
//
string boundary = DateTime.Now.Ticks.ToString("x");
HttpWebRequest uploadRequest = (HttpWebRequest)WebRequest.Create(url);//url为上传的地址
uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary;
uploadRequest.Method = "POST";
uploadRequest.Accept = "*/*";
uploadRequest.KeepAlive = true;
uploadRequest.Headers.Add("Accept-Language", "zh-cn");
uploadRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
uploadRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
if (cookies != null)
{
uploadRequest.CookieContainer = new CookieContainer();
uploadRequest.CookieContainer.Add(cookies);
}
HttpWebResponse reponse;
//创建一个内存流
Stream memStream = new MemoryStream();
//确定上传的文件路径
if (!String.IsNullOrEmpty(file))
{
boundary = "--" + boundary;
//添加上传文件参数格式边界
string paramFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}\r\n";
NameValueCollection param = new NameValueCollection();
param.Add("rule", "productImageRule");
string name = Path.GetFileName(file);
param.Add("fname", name);
//写上参数
foreach (string key in param.Keys)
{
string formitem = string.Format(paramFormat, key, param[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
//添加上传文件数据格式边界
string dataFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n";
string header = string.Format(dataFormat, "Filedata", Path.GetFileName(file));
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
//获取文件内容
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
//将文件内容写进内存流
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
//添加文件结束边界
byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n\n" + boundary + "\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\nSubmit Query\r\n" + boundary + "--");
memStream.Write(boundarybytes, 0, boundarybytes.Length);
//设置请求长度
uploadRequest.ContentLength = memStream.Length;
//获取请求写入流
Stream requestStream = uploadRequest.GetRequestStream();
//将内存流数据读取位置归零
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
//将内存流中的buffer写入到请求写入流
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
reponse = uploadRequest.GetResponse() as HttpWebResponse;
Stream st = reponse.GetResponseStream();
if (reponse.ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress);
}
StreamReader oReader = new StreamReader(st, Encoding.Default);
httpWebContent = oReader.ReadToEnd();
}
return httpWebContent;
}
贴上代码,各位高人觉得问题出在哪里? --------------------编程问答-------------------- 友情支持下 --------------------编程问答-------------------- 做新浪微博登陆遇到一些问题,就是httpwebrequset post 数据进行登陆的时候总是登陆不上不知是何原因,不知楼主遇没遇到过类似问题 --------------------编程问答-------------------- 是不是你post参数提交不完整啊 --------------------编程问答-------------------- 打下断点看下是不是缓存大小不够用的问题不 --------------------编程问答-------------------- 我是用httpwatch抓到的一个很长的字符串 string str = "entry=account&gateway=1&from=''&savestate=30&useticket=0&pagerefer=''&vsnf=1&su=用户名(base64加密后的)&service=sso&servertime=1386235469&nonce=KHWR3L&pwencode=rsa2&rsakv=1330428213&sp="rsa加密后的用户密码"&encoding=UTF-8&prelt=77&callback=parent.sinaSSOController.loginCallBack&returntype=IFRAME&setdomain=1";
将其编码写入数据流,但是就是不能得到成功的登陆页面
补充:.NET技术 , C#