httpwebresponse自带的ContentLength属性我看了下全是ffffffff,不知道是怎么回事,GetResponseStream无法使用Length,异常提示不能查找,如果我只想获取字节数组,不想用streamReader怎么设置bytes的长度?因为如果GZIP压缩过我怕获取到字符串再用内存流会丢失数据,图片我这么测试过,会丢失数据的,gzip压缩的没有测试,应该不会丢失吧?
我用httpwebrequest和httpwebresponse的时候发现有时候好像有问题。开始我用的.net4.0结果每次post一次后就不能用了,换了2.0后此问题解决了。现在是第一次登陆discuz论坛发帖正常,但是再访问发帖页面(因为要获取此页面的formHash)z在GetResponse的时候就提示超时。
我是用5d6d论坛测试的,我看到本身是开启了GZIP,我就加上了GZIP,可是在第一次发帖POST的时候就在GetRequestStream的时候提示超时,这里怎么会超时呢?又没有连接呢。
代码如下
public void Post(string fid, string title, string content, string tags)
{
HttpWebRequest request1 = (HttpWebRequest)HttpWebRequest.Create(domain + "/postzV3e.php?action=newthread&fid=" + fid + "&referer=http%3A//175888.5d6d.com/forumdisplay.php%3Ffid%3D2");
request1.CookieContainer = cookies;
request1.AllowAutoRedirect = false;
request1.Headers.Add("Accept-Language: zh-CN");
SetHeaderValue(request1.Headers, "Connection", "Keep-Alive");
request1.Headers.Add("Accept-Encoding: gzip, deflate");
request1.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request1.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPTcyRjA0RUQxRDIzOTQzNUQ5NThEN0RCQzU0QjA1QjM0JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=6eeba67a2c6ecf5a12a3766ac2195a01; .NET4.0C; .NET4.0E; 360SE)";
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
Stream s = response1.GetResponseStream();
byte[] data1 = new byte[100000];
GZipStream gzip = new GZipStream(response1.GetResponseStream(), CompressionMode.Decompress);
gzip.Read(data1, 0, data1.Length);
string html = Encoding.GetEncoding("gb2312").GetString(data1);
string[] rformhash = Match("(?<=value = )\".*?\" (\\+ \".*?\")*?(?=;)", html);
StringBuilder sbFormhash = new StringBuilder();
foreach (char item in rformhash[0])
{
if (item != '"' && item != '+' && item != ' ')
{
sbFormhash.Append(item);
}
}
string formhash = sbFormhash.ToString();
////////////////////////////////////////////////////////////////////////////////////////////////////
Uri uri = new Uri(domain + "/postzV3e.php?&action=newthread&fid=" + fid + "&extra=&topicsubmit=yes1");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ContentType = "multipart/form-data; boundary=---------------------------7dc1c92410f17f2";
request.CookieContainer = cookies;
request1.Headers.Add("Accept-Encoding: gzip, deflate");
request.AllowAutoRedirect = false;
request.Headers.Add("Accept-Language: zh-CN");
request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPTcyRjA0RUQxRDIzOTQzNUQ5NThEN0RCQzU0QjA1QjM0JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=6eeba67a2c6ecf5a12a3766ac2195a01; .NET4.0C; .NET4.0E; 360SE)";
string postStr = CreatePostStr(formhash, title, content, tags);
byte[] data = Encoding.GetEncoding("gb2312").GetBytes(postStr);
request.ContentLength = data.Length;
request.Method = "POST";
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Flush();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
在开启GZIP的情况下,我没有办法获取返回的字节数,这个怎么解决?如果不开启GZIP就会在第二次GET发帖页面的时候超时,如果开启,第一次POST之前GetRequestStream的时候就会超时。这个可能是哪里的问题?谁能帮帮我啊谢谢了
我用httpwebrequest和httpwebresponse的时候发现有时候好像有问题。开始我用的.net4.0结果每次post一次后就不能用了,换了2.0后此问题解决了。现在是第一次登陆discuz论坛发帖正常,但是再访问发帖页面(因为要获取此页面的formHash)z在GetResponse的时候就提示超时。
我是用5d6d论坛测试的,我看到本身是开启了GZIP,我就加上了GZIP,可是在第一次发帖POST的时候就在GetRequestStream的时候提示超时,这里怎么会超时呢?又没有连接呢。
代码如下
public void Post(string fid, string title, string content, string tags)
{
HttpWebRequest request1 = (HttpWebRequest)HttpWebRequest.Create(domain + "/postzV3e.php?action=newthread&fid=" + fid + "&referer=http%3A//175888.5d6d.com/forumdisplay.php%3Ffid%3D2");
request1.CookieContainer = cookies;
request1.AllowAutoRedirect = false;
request1.Headers.Add("Accept-Language: zh-CN");
SetHeaderValue(request1.Headers, "Connection", "Keep-Alive");
request1.Headers.Add("Accept-Encoding: gzip, deflate");
request1.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request1.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPTcyRjA0RUQxRDIzOTQzNUQ5NThEN0RCQzU0QjA1QjM0JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=6eeba67a2c6ecf5a12a3766ac2195a01; .NET4.0C; .NET4.0E; 360SE)";
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
Stream s = response1.GetResponseStream();
byte[] data1 = new byte[100000];
GZipStream gzip = new GZipStream(response1.GetResponseStream(), CompressionMode.Decompress);
gzip.Read(data1, 0, data1.Length);
string html = Encoding.GetEncoding("gb2312").GetString(data1);
string[] rformhash = Match("(?<=value = )\".*?\" (\\+ \".*?\")*?(?=;)", html);
StringBuilder sbFormhash = new StringBuilder();
foreach (char item in rformhash[0])
{
if (item != '"' && item != '+' && item != ' ')
{
sbFormhash.Append(item);
}
}
string formhash = sbFormhash.ToString();
////////////////////////////////////////////////////////////////////////////////////////////////////
Uri uri = new Uri(domain + "/postzV3e.php?&action=newthread&fid=" + fid + "&extra=&topicsubmit=yes1");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ContentType = "multipart/form-data; boundary=---------------------------7dc1c92410f17f2";
request.CookieContainer = cookies;
request1.Headers.Add("Accept-Encoding: gzip, deflate");
request.AllowAutoRedirect = false;
request.Headers.Add("Accept-Language: zh-CN");
request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPTcyRjA0RUQxRDIzOTQzNUQ5NThEN0RCQzU0QjA1QjM0JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=6eeba67a2c6ecf5a12a3766ac2195a01; .NET4.0C; .NET4.0E; 360SE)";
string postStr = CreatePostStr(formhash, title, content, tags);
byte[] data = Encoding.GetEncoding("gb2312").GetBytes(postStr);
request.ContentLength = data.Length;
request.Method = "POST";
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Flush();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
在开启GZIP的情况下,我没有办法获取返回的字节数,这个怎么解决?如果不开启GZIP就会在第二次GET发帖页面的时候超时,如果开启,第一次POST之前GetRequestStream的时候就会超时。这个可能是哪里的问题?谁能帮帮我啊谢谢了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货