Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同时多次打开url,就像c上的fiddler一样#_C#_Visual Studio - Fatal编程技术网

C# 同时多次打开url,就像c上的fiddler一样#

C# 同时多次打开url,就像c上的fiddler一样#,c#,visual-studio,C#,Visual Studio,我需要在同一时间多次打开一个链接,但是链接必须在网络上设置cookies,我完成了这部分,但我不能像fiddler一样重复打开的url,你能帮我吗 string url = "https://www.google.com.mx"; Uri target = new Uri(url); CookieContainer gaCookies = new CookieContainer(); gaCookies.Add(new Cookie("dosid", textBox1.Text) { Domai

我需要在同一时间多次打开一个链接,但是链接必须在网络上设置cookies,我完成了这部分,但我不能像fiddler一样重复打开的url,你能帮我吗

string url = "https://www.google.com.mx";
Uri target = new Uri(url);
CookieContainer gaCookies = new CookieContainer();
gaCookies.Add(new Cookie("dosid", textBox1.Text) { Domain = target.Host });
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.CookieContainer = gaCookies;
HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();

下面是一个使用wait/async实现的简单方法。你真的不想同时做太多。最好实现一种使用信号量的方法,或者一次只允许处理这么多信号量。但这符合您的要求:

private async void button1_Click(object sender, EventArgs e)
{

    List<string> urls = new List<string>
    {
        "https://www.google.com.mx",
        "https://www.google.com.mx",
        "https://www.google.com.mx",
        "https://www.google.com.mx",
    };


    var tasks = new List<Task>();
    var results = new List<string>();

    foreach (string url in urls)
    {
        var webReq = (HttpWebRequest)WebRequest.Create(url);
        CookieContainer gaCookies = new CookieContainer();
        gaCookies.Add(new Cookie("dosid", textBox1.Text) { Domain = /*target.Host*/ "google.com" });
        webReq.CookieContainer = gaCookies;

        tasks.Add(GetURLContentsAsync(webReq));
    }

    await Task.WhenAll(tasks); //wait for all the urls to finish loading

    foreach (Task<string> task in tasks) //get the results of all the web requests
    {
        results.Add(await task);
    }

}

private async Task<string> GetURLContentsAsync(HttpWebRequest webReq)
{
    var content = new MemoryStream();

    using (WebResponse response = await webReq.GetResponseAsync())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            await responseStream.CopyToAsync(content);
        }
    }
    var buffer = content.ToArray();
    return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
}
private async void按钮1\u单击(对象发送方,事件参数e)
{
列表URL=新列表
{
"https://www.google.com.mx",
"https://www.google.com.mx",
"https://www.google.com.mx",
"https://www.google.com.mx",
};
var tasks=新列表();
var results=新列表();
foreach(url中的字符串url)
{
var webReq=(HttpWebRequest)WebRequest.Create(url);
CookieContainer gaCookies=新CookieContainer();
添加(新Cookie(“dosid”,textBox1.Text){Domain=/*target.Host*/“google.com”});
webReq.CookieContainer=gaCookies;
tasks.Add(GetURLContentsAsync(webReq));
}
wait Task.WhenAll(tasks);//等待所有URL完成加载
foreach(Task中的Task)//获取所有web请求的结果
{
结果。添加(等待任务);
}
}
专用异步任务GetURLContentsAsync(HttpWebRequest webReq)
{
var content=newmemoryStream();
使用(WebResponse response=wait webReq.GetResponseAsync())
{
使用(Stream responseStream=response.GetResponseStream())
{
等待响应ream.CopyToAsync(内容);
}
}
var buffer=content.ToArray();
返回Encoding.UTF8.GetString(buffer,0,buffer.Length);
}

名称目标在实际上下文中不存在名称GetURLContentsAsync在实际上下文帮助中不存在!目标来自您的代码,我想您应该像处理现有代码一样处理它。我把它改为“google.com”,但它必须与你构建cookie的url相匹配。在本例中,它是“google.com”。至于GetURLContentsAsync。。。请参见示例代码。这个功能就在那里。您复制和粘贴时可能没有包含它。。。。。。我稍微修改了上面的代码以修复“目标”问题watch如何实时执行代码?为什么我对函数一无所知