using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.Net.NetworkInformation;
public class ThreadDemo
{
public static void Main()
{
List<Thread> tls = new List<Thread>();
System.Net.ServicePointManager.DefaultConnectionLimit = ;//最大并发数,.net默认并发只有2个
for (int i = ; i < ; i++)
{
tls.Add(new Thread(new ThreadStart(test)));
tls[i].Name = "线程" + (i + );
tls[i].Start();
}
}
public static void test()
{
new RequestTest().GetRequest(Thread.CurrentThread.Name, "http://163.com");
}
} public class RequestTest
{
System.Net.HttpWebRequest httpReq = null;
System.Net.HttpWebResponse httpRes = null;
/// <summary>
/// 通过WebRequest来访问网址,网址必须包含http:或https:
/// </summary>
/// <param name="threadName">当前线程</param>
/// <param name="url">要访问的网址</param>
public void GetRequest(string threadName, string url)
{
if (string.IsNullOrEmpty(url) || url.Length <= ) return;
DateTime dt = DateTime.Now;
if (!url.Contains("."))
{
return;
}
if (!url.Contains("http://") && !url.Contains("https://"))
url = "http://" + url;
try
{
httpReq = (HttpWebRequest)WebRequest.Create(url);
httpReq.Proxy = null;//不使用代理 .NET4.0中的默认代理是开启的
httpReq.KeepAlive = false;//不建立持久性连接
httpReq.Timeout = ;//连接网址的超时时间
httpReq.ReadWriteTimeout = ;//读取网址内容的超时时间
httpRes = (HttpWebResponse)httpReq.GetResponse();
Console.WriteLine(threadName + " " + url + ":" + httpRes.StatusCode);
}
catch (Exception e)
{
Console.WriteLine(threadName + " " + url + " Error:" + e.Message);
}
finally
{
if (httpRes != null)
{
httpRes.Close();//关闭连接
}
if (httpReq != null)
{
httpReq.Abort();//中止请求
}
httpReq = null;
httpRes = null;
System.GC.Collect();//强制垃圾回收,并释放资源
}
}
}

参考:

http://www.cnblogs.com/i80386/archive/2013/01/11/2856490.html

DefaultConnectionLimit 并发

http://blogs.msdn.com/b/wenlong/archive/2009/02/08/why-only-two-concurrent-requests-for-load-testing.aspx

Proxy

http://stackoverflow.com/questions/7325572/c-webrequest-proxy-null-side-effects

http://blog.sina.com.cn/s/blog_5fc933730100w3xz.html

WebRequest多线程 超时问题的更多相关文章

  1. JAVA多线程超时加载当网页图片

    先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: “http://xxxx/xxxx/xxx.jpg” 'http: ...

  2. libcurl多线程超时设置不安全(转)

    from http://www.cnblogs.com/kex1n/p/4135263.html (1), 超时(timeout) libcurl 是 一个很不错的库,支持http,ftp等很多的协议 ...

  3. java中异步多线程超时导致的服务异常

    在项目中为了提高大并发量时的性能稳定性,经常会使用到线程池来做多线程异步操作,多线程有2种,一种是实现runnable接口,这种没有返回值,一种是实现Callable接口,这种有返回值. 当其中一个线 ...

  4. C#中的多线程超时处理实践

    最近我正在处理C#中关于timeout行为的一些bug.解决方案非常有意思,所以我在这里分享给广大博友们. 我要处理的是下面这些情况: 我们做了一个应用程序,程序中有这么一个模块,它的功能向用户显示一 ...

  5. libcurl多线程超时设置不安全

    from http://blog.csdn.net/sctq8888/article/details/10031219 (1), 超时(timeout) libcurl 是 一个很不错的库,支持htt ...

  6. WebClient的超时问题及解决

    WebClient的超时问题及解决 转自:http://blog.163.com/xiaozhi797@126/blog/static/62440288201112245345838/   Webcl ...

  7. java 抓取网页图片

    import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...

  8. 基于数据库的自动化生成工具,自动生成JavaBean、自动生成数据库文档等(v4.1.2版)

            目录:            第1版:http://blog.csdn.net/vipbooks/article/details/51912143            第2版:htt ...

  9. selenuim爬虫实战(日lofter.com)

    LOFTER是网易公司2011年8月下旬推出的一款轻博客产品. LOFTER专注于为用户提供简约.易用.有品质.重原创的博客工具.原创社区,以及有品质的手机博客应用. LOFTER首次采用独立域名,口 ...

随机推荐

  1. type与instance区别

    class Foo(object): pass class Bar(Foo): pass obj = Bar() # isinstance用于判断,对象是否是指定类或其派生类的实例 print(isi ...

  2. JS (function (window, document, undefined) {})(window, document)的真正含义

    原文地址:What (function (window, document, undefined) {})(window, document); really means 按原文翻译 在这篇文章中,我 ...

  3. java 不通过第三个字符串,实现一个字符串倒序

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha String s="abcde"; String  s2 = new ...

  4. bzoj1814: Ural 1519 Formula 1 动态规划 插头dp

    http://acm.timus.ru/problem.aspx?space=1&num=1519 题目描述 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数. ...

  5. CSS选择器复习

    通用选择器:* 选择到所有的元素 选择子元素:> 选择到元素的直接后代(第一级子元素) 相邻兄弟选择器:+ 选择到紧随目标元素后的第一个元素 普通兄弟选择器:~ 选择到紧随其后的所有兄弟元素 伪 ...

  6. X-010 FriendlyARM tiny4412 uboot移植之移植网卡驱动TFTP用起来

    <<<<<<<<<<<<<<<<<<<<<<<<< ...

  7. chrome 浏览器 console 加入 jquery 测试调试 一介布衣

    chrome 浏览器 console 加入 jquery 测试调试 一介布衣   var jquery = document.createElement('script'); jquery.src = ...

  8. java linux 项目常常无故被关闭 进程无故消息

    布了几个项目.竟然天天会自己主动的挂掉.急了.花时间攻克了一下.总结方案例如以下: 1.磁盘满了.这大家都懂,清一下 2.tomcat在关闭的或是重新启动的时候,经常后台进程没有被关闭.须要用ps a ...

  9. .net 基于Jenkins的自动构建系统开发

    先让我给描述一下怎么叫一个自动构建或者说是持续集成 : 就拿一个B/S系统的合作开发来说,在用SVN版本控制的情况下,每个人完成自己代码的编写,阶段性提交代码,然后测试-修改,最后到所有代码完工,进行 ...

  10. systemtap 用户态调试

    #include <stdio.h> int main( void) { ; a=fun(,); printf("%d\n",a); } int fun(int a,i ...