标签: system.net.httpwebrequest

读取 HttpWebResponse 时获取“流不可读”

我正在尝试“屏幕抓取”一些数据我有一个请求如下(来自提琴手)

POST http://fallenlondon.storynexus.com/Auth/EmailLogin HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Referer: http://fallenlondon.storynexus.com/signup
User-Agent: Mine
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Cache-Control: max-age=0
l: 
Origin: http://fallenlondon.storynexus.com/
DNT: 1
Accept-Encoding: utf-8
Accept-Language: en-GB,en;q=0.8
Cookie: ASP.NET_SessionId=05xq3gndu4nczvy5wsah5qyw; __utma=100212060.1740063036.1431282067.1431282067.1431284767.2; __utmb=100212060.14.10.1431284767; __utmc=100212060; __utmz=100212060.1431282067.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host: fallenlondon.storynexus.com
Content-Length: 54
Run Code Online (Sandbox Code Playgroud)

(内容是我的凭据) - 此标头与我在浏览器中手动查看网页所跟踪的请求相匹配。

我使用 HttpWebRequest.GetResponse() 发送这个我得到了回复

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache, no-store, must-revalidate
Content-Type: text/html; charset=utf-8
Date: Mon, 11 May 2015 20:54:15 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 4.0
X-Powered-By: ASP.NET
X-Server: Web1
Content-Length: 16900
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

这(再次)匹配我使用浏览器获得的内容。使用 fiddler,我可以看到 17k 的数据(html),我尝试使用... …

c# http c#-4.0 system.net.httpwebrequest

5
推荐指数
1
解决办法
8801
查看次数

使用DefaultWebProxy获取IE的默认代理

我已经阅读了几乎所有可以找到的文档,但我还没有找到一个简单的工作示例,说明如何使用IE的默认代理设置DefaultWebProxy().

这段代码似乎编译和工作,但我如何继续将代理URI作为字符串?

HttpWebRequest webRequest = 
    (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");

if (WebRequest.DefaultWebProxy != null)
{
    webRequest.Proxy = WebRequest.DefaultWebProxy;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

自提交此问题以来,我发现可以为不同的目的地设置一个或多个代理,或者绕过(可能是本地Intranet目的地).这就是你需要指定URI的原因GetProxy().它需要知道获取代理的目的地.如果在"Internet选项"中设置了"自动检测设置",则浏览器将在本地域中查找PAC文件.PAC文件包含一个Javascript函数,用于确定给定目标的代理地址.

c# proxy system.net system.net.httpwebrequest

4
推荐指数
1
解决办法
1万
查看次数

创建通用异步任务功能

我创建了一个使用async/ 返回对象的函数await.我想使函数通用,以便它可以返回我传入的任何对象.除了返回的对象之外,代码是样板文件.我希望能够调用GetAsync并让它返回正确的对象

public Patron getPatronById(string barcode)
{
    string uri = "patrons/find?barcode=" + barcode;
    Patron Patron =  GetAsync(uri).Result;
    return Patron;
}

private async Task<Patron> GetAsync(string uri)
{
    var client = GetHttpClient(uri);
    var content = await client.GetStringAsync(uri);
    JavaScriptSerializer ser = new JavaScriptSerializer();
    Patron Patron = ser.Deserialize<Patron>(content);
    return Patron;
}
Run Code Online (Sandbox Code Playgroud)

c# generics asp.net-web-api system.net.httpwebrequest

2
推荐指数
1
解决办法
2711
查看次数

如何将System.Net.HttpWebResponse中的cookie携带到下一个System.Net.HttpWebRequest?

我正在尝试做一些自动化的Web请求,需要从一个到另一个维护一个cookie.我可以看到我从初始响应中获取了我想要的cookie,但我无法将其附加到下一个请求中.

c#代码

// response part

using (var wresp = (System.Net.HttpWebResponse)wrequest.GetResponse())
{

    // respblob gets returned and is accessible to the next request
    respblob.CookieList = new List<System.Net.Cookie>(); 

    foreach (System.Net.Cookie cook in wresp.Cookies)
    {
        respblob.CookieList.Add(cook);
    }

    // ... more stuff not related to cookies
}


// next request part
   var wrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);

    wrequest.Method = "POST";
    wrequest.ContentType = "application/x-www-form-urlencoded";

    wrequest.CookieContainer = new System.Net.CookieContainer();

    // request.CookieList contains one cookie as expected 
    // from the previous response
    for (int j = 0; j < …
Run Code Online (Sandbox Code Playgroud)

c# cookies system.net.httpwebrequest

1
推荐指数
1
解决办法
7380
查看次数