using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Newtonsoft.Json.Linq; namespace TestKaiPing
{
class Program
{
private static long _totalTimeCost = ;
private static int _endedConnenctionCount = ;
private static int _failedConnectionCount = ;
private static int _connectionCount = ; static void Main(string[] args)
{
var connectionCount = ;
var requestThread = new Thread(() => StartRequest(connectionCount)) {IsBackground = true};
requestThread.Start(); Console.WriteLine("恭喜,成功完成!");
Console.ReadLine();
} private static void SendRequest()
{
try
{
var url =
"http://kpsso.kpedu.com/ajaxLogin?targetService=http://221.194.113.150/dsideal_yy/html/ypt/getLoginInfo.jsp&callback=loginFormState&userName=dscsjs1&password=123456";
var r = SendRequestByGet(url);
r = r.Replace("loginFormState(", "").Replace(")", "");
var o = JObject.Parse(r);
var ticket = o["ticket"]; url = "http://221.194.113.150/dsideal_yy/html/ypt/getLoginInfo.jsp?ticket=" + ticket;
r = SendRequestByGet(url); IncreaseSuccessConnection();
}
catch (Exception)
{
IncreaseFailedConnection();
}
}
private static void Reset()
{
_failedConnectionCount = ;
_endedConnenctionCount = ;
_totalTimeCost = ;
} private static void IncreaseFailedConnection()
{
Interlocked.Increment(ref _failedConnectionCount);
Console.WriteLine("失败个数:"+ _failedConnectionCount);
} private static void IncreaseSuccessConnection()
{
Interlocked.Increment(ref _endedConnenctionCount);
Console.WriteLine("成功个数:"+ _endedConnenctionCount);
} private static void StartRequest(int connectionCount)
{
Reset();
for (var i = ; i < connectionCount; i++)
{
ThreadPool.QueueUserWorkItem(u => SendRequest());
}
}
public static string SendRequestByGet(string uri)
{
string fullhtml = null;
while (true)
{
try
{
var req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "GET";
req.ServicePoint.ConnectionLimit = int.MaxValue;
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
req.KeepAlive = true;
req.Timeout = * ;
var resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode != HttpStatusCode.OK) //如果服务器未响应,那么继续等待相应
continue;
var sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
fullhtml = sr.ReadToEnd().Trim();
resp.Close();
sr.Close();
break;
}
catch (WebException e)
{
e.StackTrace.ToString();
Trace.WriteLine(e.Message);
if (true)
continue;
}
}
return fullhtml;
}
}
}

最新文章

  1. 从零开始学 Java - 我放弃了 .NET ?
  2. JS组件系列——Bootstrap Table 表格行拖拽(二:多行拖拽)
  3. sql逻辑执行顺序
  4. Codeforces 676C Vasya and String(尺取法)
  5. Fiddler---前端利器
  6. 敏捷软件开发 原则 模式 与实践 - OCP原则
  7. 在rails下新建表
  8. sopcinfo路径改变,nios工程该怎么办?
  9. MongoDb 2.4 beta新特性——全文索引
  10. log4net 将日志写入数据库
  11. 基于visual Studio2013解决算法导论之023队列实现(基于数组)
  12. VMWARE虚拟机无法访问的三种方法分析
  13. As input tri-stated
  14. IDEA 2 的注册码
  15. ssm简单搭建
  16. [Swift]LeetCode295. 数据流的中位数 | Find Median from Data Stream
  17. IOS解析XML文件
  18. Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程
  19. JavaScript -基础- 函数与对象(三)数组对象
  20. VMware下ubuntu与Windows实现文件共享的方法 (转)

热门文章

  1. Android stadio butternife工具
  2. 读json文件发生错误,所遇到的坑
  3. 10.bootstrap分页,点击哪个分页号,哪个分页号就active
  4. 手把手教你如何逐步安装OpenStack
  5. 《Java程序员由笨鸟到菜鸟》
  6. iOS程序执行顺序和UIViewController 的生命周期(整理)
  7. 从事IT业一个8年老兵转行前的自我总结2——从《易经》说开来
  8. JavaScript中最常用的55个经典技巧,没事的时候看看,拓展解决问题的思路
  9. 关于MySQL查询优化 の 30条忠告
  10. django-settings里mysql连接配置