标签: httpwebresponse

HttpWebResponse响应流的最佳缓冲区大小

与来自HttpWebResponse.GetResponseStream()的流一起使用的最佳缓冲区大小是多少?

在线示例从256b到5Kb不等.是什么赋予了?我想缓冲区大小可能是情境化的.如果是这样,使用什么类型的缓冲区大小的情况是什么?

谢谢.

.net buffer httpwebresponse

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

如何使用.NET HttpWebRequest API从响应中读取HTTP头?

我的应用目前使用OAuth与Twitter API进行通信.早在去年12月,Twitter就将OAuth的费率上限提高到每小时350个请求.但是,我没有看到这一点.我仍然从account/rate_limit_status方法获得150 .

有人告诉我,我需要使用X-RateLimit-LimitHTTP标头来获得新的速率限制.但是,在我的代码中,我没有看到标题.

这是我的代码......

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newURL);
request.Method = "GET";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";

using (WebResponse response = request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        responseString = reader.ReadToEnd();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我检查response,我可以看到它有一个属性Headers,并有16个标题.但是,我没有X-RateLimit-Limit在列表中.

图片http://img10.yfrog.com/img10/5997/33201085434am.png

知道我做错了什么吗?

c# twitter oauth httpwebresponse

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

HttpWebRequest的并发限制

我正在编写一个应用程序来测量我使用C#下载网页的速度.我提供了一个唯一域名列表,然后我生成X个线程并执行HTTPWebRequests,直到消耗了域列表.问题是无论我使用多少线程,我每秒只能获得大约3页.

我发现System.Net.ServicePointManager.DefaultConnectionLimit是2,但我的印象是这与每个域的连接数有关.由于列表中的每个域都是唯一的,因此这不应成为问题.

然后我发现GetResponse()方法阻止来自所有其他进程的访问,直到关闭WebResponse:http://www.codeproject.com/KB/IP/Crawler.aspx#WebRequest,我还没有找到任何关于web支持这个声明,但我使用套接字实现了一个HTTP请求,我注意到了一个显着的加速(4x到6x).

所以我的问题是:有没有人确切知道HttpWebRequest对象是如何工作的?除了上面提到的内容之外还有解决方法吗?还是有任何用C#编写的高速网络爬虫的例子?

.net c# multithreading httpwebrequest httpwebresponse

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

获取错误"使用HttpWebRequest.GetResponse()进行屏幕抓取时,远程服务器返回错误:(403)禁止"

我们有一个工具可以检查给定的URL是否是实时URL.如果给定的网址是实时的,我们软件的另一部分可以屏蔽其中的内容.

这是我检查网址是否有效的代码

    public static bool IsLiveUrl(string url)
    {
        HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
        webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5";
        webRequest.CookieContainer = new CookieContainer();
        WebResponse webResponse;
        try
        {
            webResponse = webRequest.GetResponse();
        }
        catch (WebException e)
        {
            return false;
        }
        catch (Exception ex)
        {

            return false;
        }
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

这段代码完美无缺,但是对于在apache上托管的特定站点,我收到了一个带有以下消息的Web异常."远程服务器返回错误:(403)Forbidden"在进一步检查时,我在WebException对象中找到了以下详细信息

Status ="ProtocolError"StatusDescription ="不良行为"

这是请求标题"User-Agent:Mozilla/5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6)Gecko/20060728 Firefox/1.5主机:scenicspares.co.uk连接:Keep-Alive "

这是响应标题"Keep-Alive:timeout = 4,max = 512 Connection:Keep-Alive Transfer-Encoding:chunked Content-Type:text/html Date:Thu,13 Jan 2011 10:29:36 …

c# screen-scraping httpwebrequest httpwebresponse http-status-code-403

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

powershell httpwebrequest GET方法cookiecontainer问题?

我正在试图抓住一个拥有用户身份验证的网站.我能够发一个POST来发送我的登录信息并存储一个cookie.但是,登录后,我在尝试访问受保护的页面时收到403错误.

$url = "https://some_url"

$CookieContainer = New-Object System.Net.CookieContainer

$postData = "User=UserName&Password=Pass"

$buffer = [text.encoding]::ascii.getbytes($postData)

[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.method = "POST"
$req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
$req.Headers.Add("Accept-Language: en-US")
$req.Headers.Add("Accept-Encoding: gzip,deflate")
$req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
$req.AllowAutoRedirect = $false
$req.ContentType = "application/x-www-form-urlencoded"
$req.ContentLength = $buffer.length
$req.TimeOut = 50000
$req.KeepAlive = $true
$req.Headers.Add("Keep-Alive: 300");
$req.CookieContainer = $CookieContainer
$reqst = $req.getRequestStream()
$reqst.write($buffer, 0, $buffer.length)
$reqst.flush()
$reqst.close()
[net.httpWebResponse] $res = $req.getResponse()
$resst = $res.getResponseStream()
$sr = new-object IO.StreamReader($resst)
$result = $sr.ReadToEnd()
$res.close()



$url2 = "https://some_url/protected_page"

[net.httpWebRequest] $req2 = [net.webRequest]::create($url2) …
Run Code Online (Sandbox Code Playgroud)

powershell httpwebrequest httpwebresponse cookiecontainer

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

c#HttpWebResponse头编码

我有以下问题.我联系了一个我知道使用301重定向的地址.

使用HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl); ,loHttp.AllowAutoRedirect = false;所以我没有重定向.

现在我得到响应的标题以识别新的URL.

运用 loWebResponse.GetResponseHeader("Location");

问题是,由于此URL包含希腊字符,因此返回的字符串全部混乱(由于编码).

完整图片代码:

HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.ContentType = "application/x-www-form-urlencoded";
loHttp.Method = "GET";

Timeout = 10000;

loHttp.AllowAutoRedirect = false;
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();

string url= loWebResponse.Headers["Location"];
Run Code Online (Sandbox Code Playgroud)

c# encoding header httpwebresponse

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

使用.NET将文件发布到服务器HttpWebRequest或WebClient

好的,这是交易.正如问题所述,我正在尝试将文件发布到网络服务器并且遇到一些问题.

我尝试使用Curl.exe将同一个文件发布到同一个Web服务器,并且没有任何问题.我已经发布了我用curl的标志,因为他们可能会指出我为什么遇到.NET类问题的任何潜在原因.

curl.exe --user "myUser:myPass" --header "Content-Type: application/gzip"  
--data-binary "@filename.txt.gz" --cookie "data=service; data-ver=2; date=20100212;  
time=0900; location=1234" --output "out.txt" --dump-header "header.txt"  
http://mysite/receive
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用像WebClient或HttpWebRequest这样的.NET类来做同样的事情.这是我尝试过的代码示例.使用WebClient,我得到505 HTTP版本不支持错误,并且使用HttpWebRequest我得到501 Not Implemented.

使用WebClient尝试时:

public void sendFileClient(string path){
    string url = "http://mysite/receive";  
    WebClient wc = new WebClient();  
    string USERNAME = "myUser";  
    string PSSWD = "myPass";  

    NetworkCredential creds = new NetworkCredential(USERNAME, PSSWD);  
    wc.Credentials = creds;  
    wc.Headers.Set(HttpRequestHeader.ContentType, "application/gzip");  
    wc.Headers.Set("Cookie", "location=1234; date=20100226; time=1630; data=service; data-ver=2");  

    wc.UploadFile(url, "POST", path);
}
Run Code Online (Sandbox Code Playgroud)

在使用HttpRequest时:

public Stream sendFile(string path)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://myserver/receive");  
    string USERNAME …
Run Code Online (Sandbox Code Playgroud)

.net c# post webrequest httpwebresponse

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

在HttpWebRequest上获得"底层连接已关闭"

我有一个用VB.NET编写的应用程序(不是 asp.net,它是一个Windows控制台应用程序).我正在尝试调用url(一个html页面)并将响应返回到一个字符串中.响应是直接的JSON,没有任何html标签.它打开{和关闭}.

我很好地创建了HttpWebRequest对象.然后打电话req.GetResponse().一旦我这样做,我得到错误,The underlying connection was closed: The connection was closed unexpectedly.我一直在谷歌搜索和检查stackoverflow,并尝试了我发现适用的一切(很多与WCF服务配置有关,不适用).

这是我的代码:

Public Function GetJSON(ByRef d As db.Device) As Boolean
    Try
        d.Url = "http://" & d.IpAddress & ini.doc.<svc>.<url>.Value

        Dim req As HttpWebRequest = HttpWebRequest.Create(d.Url)
        // req.Accept = "*/*"
        // req.Timeout = 30000
        // req.ReadWriteTimeout = 30000
        // req.KeepAlive = False
        // req.UseDefaultCredentials = True
        // req.CachePolicy = HttpWebRequest.DefaultCachePolicy
        // req.Proxy = HttpWebRequest.DefaultWebProxy
        // req.ProtocolVersion = New System.Version(1, 0)

        Dim rsp …
Run Code Online (Sandbox Code Playgroud)

httpwebrequest httpwebresponse system.net.webexception

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

HttpWebResponse

我想从服务器下载图像.当图像不存在时,我想显示我的默认图像.

这是我的代码:

string url = "http://www......d_common_conference" + "/" + c.id_common_conference + "-MDC.jpg";

try {
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "HEAD";                        
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    string status = Response.StatusCode.ToString();                                               

    img.ImageUrl = url;
}
catch (Exception excep) {
    img.ImageUrl = "images/silhouete.jpg";
    string msg = excep.Message;
} 
Run Code Online (Sandbox Code Playgroud)

它运行良好,但直到第24个循环,没有响应,没有异常抛出,我的程序变得卡住.

我怎样才能解决这个问题?

c# asp.net httpwebresponse

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

分段C#文件下载器

一直在尝试用C#编写程序,像大多数下载管理器一样使用多个段下载文件,我遇到了下载的文件已损坏的问题.例如,我下载一个视频,它播放2秒,然后WMP说它无法播放.

我对下载的文件进行了咒骂,看起来整个文件中都有零散的部分,任何人有什么想法?VS报告没有错误.

getPart() 在单独的线程中为每个段调用.

public long start;
public long end;
public int thread;
public Form1 handle;
public myFile handler;
public void getPart()
{
    log("getting part " + start.ToString() + "," + end.ToString());
    HttpWebRequest part = (HttpWebRequest)WebRequest.Create(handler.url);
    part.AddRange((int)start,(int) end);
    HttpWebResponse pr = (HttpWebResponse)part.GetResponse();
    Stream rstream = pr.GetResponseStream();
    log("Beginning part " + start.ToString());
    int totalbytes = 0;
    byte[] buffer = new byte[256];
    int x = rstream.Read(buffer, 0, 256);
    while (x > 0)
    {
        handler.writeFile(buffer, (int)(totalbytes + start), x);
        totalbytes += x;
        x …
Run Code Online (Sandbox Code Playgroud)

c# download httpwebresponse

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