HttpWebRequest多线程设置了cookie之后卡死,只有一个线程运行

hglai 2015-12-23 11:08:15
Winform HttpWebRequest同时5个线程在传文件,当设置了cookie时,就同时只有一个线程在传,其他线程卡在哪里,为什么

if (cookie != null)
httpReq.Headers.Add("Cookie", cookie);//设置cookies
else
httpReq.Headers.Add("Cookie", cookiePublic);//设置cookies
如果每个线程设置一个不同的cookie,也就是每个线程都登录一次,就不会,如果5个线程公用一个cookie,就会卡死,晕死
...全文
254 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdashewan 2015-12-28
  • 打赏
  • 举报
回复
引用 10 楼 hglai 的回复:
没有用过监控工具,不知道怎么用啊,有教程吗,学习下
搜索wireshark,教程什么的也有很多
hglai 2015-12-25
  • 打赏
  • 举报
回复
没有用过监控工具,不知道怎么用啊,有教程吗,学习下
xdashewan 2015-12-25
  • 打赏
  • 举报
回复
引用 8 楼 hglai 的回复:
有的时候会整体卡死
不行就使用监控工具,看握手过程和传输过程
hglai 2015-12-24
  • 打赏
  • 举报
回复
效果如下,同时开始的线程,有一个往下运行,其他的都等待


多等下以后就会发生如下错误


有的时候会整体卡死
hglai 2015-12-24
  • 打赏
  • 举报
回复
有人碰到同样的问题吗?求助呀
hglai 2015-12-24
  • 打赏
  • 举报
回复
确定没有,如果每个线程都用同一个用户登陆一次,也就是产生不同的cookies,那么就没问题 这样的ip也是相同的 服务器只是限制了要登陆才能传文件,其他的没有限制。
xdashewan 2015-12-24
  • 打赏
  • 举报
回复
你确定服务器没有限制同一时间同一个用户只能上传一个文件吗?你可以用监控tcp工具监控下整个传输过程,看看哪里出问题了
hglai 2015-12-24
  • 打赏
  • 举报
回复
不会报错,就是会卡住,进度不变,只有一个是变的, 以开始是用CookieContainer ,后来改用在header里加入cookie也不行,如果不加cookie,就没有问题,加不同的cookie也没有问题,顺便贴上代码: private static string Upload_Request(string address, Stream fs, string saveName, UpdateGridMethod updateGrid, DataGridViewRow row,string cookie) { // 要上传的文件 //FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); System.Net.ServicePointManager.DefaultConnectionLimit = 50; BinaryReader r = new BinaryReader(fs, Encoding.UTF8); string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");//时间戳 byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n"); StringBuilder sb = new StringBuilder();//请求头部信息 sb.Append("--"); sb.Append(strBoundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("xmldata"); sb.Append("\"; filename=\""); sb.Append(saveName); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string strPostHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); // 根据uri创建HttpWebRequest对象 System.GC.Collect(); HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url + address)); //----优化参数 httpReq.ServicePoint.Expect100Continue = false; httpReq.ServicePoint.UseNagleAlgorithm = false; httpReq.ServicePoint.ConnectionLimit = 50; httpReq.AllowWriteStreamBuffering = false; httpReq.Proxy = null; httpReq.Method = "POST"; if (cookie != null) httpReq.Headers.Add("Cookie", cookie);//设置cookies else httpReq.Headers.Add("Cookie", cookiePublic);//设置cookies //httpReq.CookieContainer = CookieContainer; httpReq.AllowWriteStreamBuffering = false;//对发送的数据不使用缓存 httpReq.Timeout = 60000;//设置获得响应的超时时间(600秒) httpReq.KeepAlive = false; httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary; long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length; long fileLength = fs.Length; httpReq.ContentLength = length; Stream postStream = null; WebResponse webRespon = null; string returnMsg = null; try { updateGrid(row, null, "正在准备开始上传", null, null); //每次上传4k int bufferLength = 4096; byte[] buffer = new byte[bufferLength]; //已上传的字节数 long offset = 0; //开始上传时间 DateTime startTime = DateTime.Now; int size = r.Read(buffer, 0, bufferLength); postStream = httpReq.GetRequestStream(); //发送请求头部消息 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); while (size > 0) { postStream.Write(buffer, 0, size); offset += size; //progressBar.Value = (int)(offset * (int.MaxValue / length)); TimeSpan span = DateTime.Now - startTime; double second = span.TotalSeconds; if (second > 0.001) { string info = (offset * 100.0 / length).ToString("F2") + "% " + (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒"; updateGrid(row, null, info, null, null);//lblState.Text = "已上传:" + (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M(" + (offset * 100.0 / length).ToString("F2") + "%) 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒"; } else { updateGrid(row, null, "正在连接…", null, null);//lblState.Text = " 正在连接…"; } size = r.Read(buffer, 0, bufferLength); } updateGrid(row, null, "已上传完成", null, null);//lblState.Text = "已上传完成"; //添加尾部的时间戳 postStream.Write(boundaryBytes, 0, boundaryBytes.Length); postStream.Close(); //获取服务器端的响应 webRespon = httpReq.GetResponse(); Stream s = webRespon.GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.UTF8); //读取服务器端返回的消息 string sReturnString = sr.ReadToEnd().Trim(); s.Close(); sr.Close(); returnMsg= sReturnString; } catch (Exception e) { returnMsg= "1" + e.Message; } finally { try { fs.Close(); r.Close(); if (postStream != null) postStream.Close(); if (webRespon != null) { webRespon.Close(); webRespon = null; } httpReq = null; } catch (Exception e) { returnMsg= e.Message; } } return returnMsg; }
hglai 2015-12-24
  • 打赏
  • 举报
回复
不会报错,就是会卡住,进度不变,只有一个是变的, 以开始是用CookieContainer ,后来改用在header里加入cookie也不行,如果不加cookie,就没有问题,加不同的cookie也没有问题,顺便贴上代码: private static string Upload_Request(string address, Stream fs, string saveName, UpdateGridMethod updateGrid, DataGridViewRow row,string cookie) { // 要上传的文件 //FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); System.Net.ServicePointManager.DefaultConnectionLimit = 50; BinaryReader r = new BinaryReader(fs, Encoding.UTF8); string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");//时间戳 byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n"); StringBuilder sb = new StringBuilder();//请求头部信息 sb.Append("--"); sb.Append(strBoundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("xmldata"); sb.Append("\"; filename=\""); sb.Append(saveName); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string strPostHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); // 根据uri创建HttpWebRequest对象 System.GC.Collect(); HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url + address)); //----优化参数 httpReq.ServicePoint.Expect100Continue = false; httpReq.ServicePoint.UseNagleAlgorithm = false; httpReq.ServicePoint.ConnectionLimit = 50; httpReq.AllowWriteStreamBuffering = false; httpReq.Proxy = null; httpReq.Method = "POST"; if (cookie != null) httpReq.Headers.Add("Cookie", cookie);//设置cookies else httpReq.Headers.Add("Cookie", cookiePublic);//设置cookies //httpReq.CookieContainer = CookieContainer; httpReq.AllowWriteStreamBuffering = false;//对发送的数据不使用缓存 httpReq.Timeout = 60000;//设置获得响应的超时时间(600秒) httpReq.KeepAlive = false; httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary; long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length; long fileLength = fs.Length; httpReq.ContentLength = length; Stream postStream = null; WebResponse webRespon = null; string returnMsg = null; try { updateGrid(row, null, "正在准备开始上传", null, null); //每次上传4k int bufferLength = 4096; byte[] buffer = new byte[bufferLength]; //已上传的字节数 long offset = 0; //开始上传时间 DateTime startTime = DateTime.Now; int size = r.Read(buffer, 0, bufferLength); postStream = httpReq.GetRequestStream(); //发送请求头部消息 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); while (size > 0) { postStream.Write(buffer, 0, size); offset += size; //progressBar.Value = (int)(offset * (int.MaxValue / length)); TimeSpan span = DateTime.Now - startTime; double second = span.TotalSeconds; if (second > 0.001) { string info = (offset * 100.0 / length).ToString("F2") + "% " + (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒"; updateGrid(row, null, info, null, null);//lblState.Text = "已上传:" + (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M(" + (offset * 100.0 / length).ToString("F2") + "%) 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒"; } else { updateGrid(row, null, "正在连接…", null, null);//lblState.Text = " 正在连接…"; } size = r.Read(buffer, 0, bufferLength); } updateGrid(row, null, "已上传完成", null, null);//lblState.Text = "已上传完成"; //添加尾部的时间戳 postStream.Write(boundaryBytes, 0, boundaryBytes.Length); postStream.Close(); //获取服务器端的响应 webRespon = httpReq.GetResponse(); Stream s = webRespon.GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.UTF8); //读取服务器端返回的消息 string sReturnString = sr.ReadToEnd().Trim(); s.Close(); sr.Close(); returnMsg= sReturnString; } catch (Exception e) { returnMsg= "1" + e.Message; } finally { try { fs.Close(); r.Close(); if (postStream != null) postStream.Close(); if (webRespon != null) { webRespon.Close(); webRespon = null; } httpReq = null; } catch (Exception e) { returnMsg= e.Message; } } return returnMsg; }
hglai 2015-12-24
  • 打赏
  • 举报
回复
不会报错,就是会卡住,进度不变,只有一个是变的, 以开始是用CookieContainer ,后来改用在header里加入cookie也不行,如果不加cookie,就没有问题,加不同的cookie也没有问题,顺便贴上代码: private static string Upload_Request(string address, Stream fs, string saveName, UpdateGridMethod updateGrid, DataGridViewRow row,string cookie) { // 要上传的文件 //FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); System.Net.ServicePointManager.DefaultConnectionLimit = 50; BinaryReader r = new BinaryReader(fs, Encoding.UTF8); string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");//时间戳 byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n"); StringBuilder sb = new StringBuilder();//请求头部信息 sb.Append("--"); sb.Append(strBoundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("xmldata"); sb.Append("\"; filename=\""); sb.Append(saveName); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string strPostHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); // 根据uri创建HttpWebRequest对象 System.GC.Collect(); HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url + address)); //----优化参数 httpReq.ServicePoint.Expect100Continue = false; httpReq.ServicePoint.UseNagleAlgorithm = false; httpReq.ServicePoint.ConnectionLimit = 50; httpReq.AllowWriteStreamBuffering = false; httpReq.Proxy = null; httpReq.Method = "POST"; if (cookie != null) httpReq.Headers.Add("Cookie", cookie);//设置cookies else httpReq.Headers.Add("Cookie", cookiePublic);//设置cookies //httpReq.CookieContainer = CookieContainer; httpReq.AllowWriteStreamBuffering = false;//对发送的数据不使用缓存 httpReq.Timeout = 60000;//设置获得响应的超时时间(600秒) httpReq.KeepAlive = false; httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary; long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length; long fileLength = fs.Length; httpReq.ContentLength = length; Stream postStream = null; WebResponse webRespon = null; string returnMsg = null; try { updateGrid(row, null, "正在准备开始上传", null, null); //每次上传4k int bufferLength = 4096; byte[] buffer = new byte[bufferLength]; //已上传的字节数 long offset = 0; //开始上传时间 DateTime startTime = DateTime.Now; int size = r.Read(buffer, 0, bufferLength); postStream = httpReq.GetRequestStream(); //发送请求头部消息 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); while (size > 0) { postStream.Write(buffer, 0, size); offset += size; //progressBar.Value = (int)(offset * (int.MaxValue / length)); TimeSpan span = DateTime.Now - startTime; double second = span.TotalSeconds; if (second > 0.001) { string info = (offset * 100.0 / length).ToString("F2") + "% " + (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒"; updateGrid(row, null, info, null, null);//lblState.Text = "已上传:" + (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M(" + (offset * 100.0 / length).ToString("F2") + "%) 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒"; } else { updateGrid(row, null, "正在连接…", null, null);//lblState.Text = " 正在连接…"; } size = r.Read(buffer, 0, bufferLength); } updateGrid(row, null, "已上传完成", null, null);//lblState.Text = "已上传完成"; //添加尾部的时间戳 postStream.Write(boundaryBytes, 0, boundaryBytes.Length); postStream.Close(); //获取服务器端的响应 webRespon = httpReq.GetResponse(); Stream s = webRespon.GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.UTF8); //读取服务器端返回的消息 string sReturnString = sr.ReadToEnd().Trim(); s.Close(); sr.Close(); returnMsg= sReturnString; } catch (Exception e) { returnMsg= "1" + e.Message; } finally { try { fs.Close(); r.Close(); if (postStream != null) postStream.Close(); if (webRespon != null) { webRespon.Close(); webRespon = null; } httpReq = null; } catch (Exception e) { returnMsg= e.Message; } } return returnMsg; }
newtee 2015-12-24
  • 打赏
  • 举报
回复
看看报什么错 可能CookieContainer不支持并发更新 需要加个全局静态锁

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧