标签: httpwebresponse

此流不支持搜索操作.HttpWebResponse

我正在制作一个通过http下载文件的程序.

我已经下载了,但是我希望能够暂停下载,关闭程序并在以后再次恢复它们.

我知道我从下载它们的位置支持这个.

我正在通过HttpWebResponse下载文件,并使用GetResponseStream将响应读入Stream.

当我关闭应用程序并重新启动它时,我不知道如何恢复下载.我已尝试在流上进行搜索,但它声明它不受支持.

最好的方法是什么?

c# download stream httpwebrequest httpwebresponse

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

如何将byte []响应转换为有效的XDocument?

<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
        <image_hash>cxmHM</image_hash>
        <delete_hash>NNy6VNpiAA</delete_hash>
        <original_image>http://imgur.com/cxmHM.png</original_image>
        <large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
        <small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
        <imgur_page>http://imgur.com/cxmHM</imgur_page>
        <delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>
Run Code Online (Sandbox Code Playgroud)

这是我将收到的典型回复.我尝试了以下但是我收到一个错误,告诉我非空白字符不能添加到内容.

XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));    
Run Code Online (Sandbox Code Playgroud)

.net c# webclient linq-to-xml httpwebresponse

6
推荐指数
2
解决办法
6735
查看次数

互联网停机时,请继续尝试与服务器通信

所以我的应用程序正在与服务器交换请求/响应(没有问题),直到互联网连接死了几秒钟,然后回来.然后是这样的代码:

response = (HttpWebResponse)request.GetResponse();

将抛出一个异常,与状态一样ReceiveFailure,ConnectFailure,KeepAliveFailure等.

现在,非常重要的是,如果互联网连接回来,我能够继续与服务器通信,否则我将不得不从头开始,这将需要很长时间.

当互联网回来时,您将如何恢复此通信?

目前,我一直在检查是否有可能与服务器通信,直到可能(至少理论上).我的代码尝试看起来像这样:

try
{
    response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    // We have a problem receiving stuff from the server. 
    // We'll keep on trying for a while
    if (ex.Status == WebExceptionStatus.ReceiveFailure || 
        ex.Status == WebExceptionStatus.ConnectFailure || 
        ex.Status == WebExceptionStatus.KeepAliveFailure)
    {
        bool stillNoInternet = true;

        // keep trying to talk to the server
        while (stillNoInternet)
        {
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                stillNoInternet = false;
            }
            catch …
Run Code Online (Sandbox Code Playgroud)

.net c# httpwebrequest httpwebresponse system.net.webexception

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

HttpWebRequest突然停止工作,几个请求后没有收到响应

我正在使用WPF .net 4.0应用程序.我有一个搜索栏.对于每个搜索令牌,我需要对8个单独的URL执行8个http请求以获取搜索结果.一旦用户停止在搜索栏中输入,我会在400毫秒后向服务器发送8个请求.搜索6到7个搜索令牌的结果非常好.但在那之后突然HttpWebRequest停止了默默工作.没有例外,没有收到任何回复.我正在使用Windows 7,我也禁用了防火墙.我不知道后续的http请求丢失在哪里.

任何人都可以向我展示灯来解决这个问题吗?

下面是我的HttpWebRequest调用代码.

public static void SendReq(string url)
{
    // Create a new HttpWebRequest object.
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    request.ContentType = "application/x-www-form-urlencoded";
    request.Proxy = new WebProxy("192.168.1.1", 8000);

    // Set the Method property to 'POST' to post data to the URI.
    request.Method = "POST";

    // start the asynchronous operation
    request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

}

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    Stream postStream = request.EndGetRequestStream(asynchronousResult);

    string postData = this.PostData;

    // Convert the …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf httpwebresponse

6
推荐指数
2
解决办法
3266
查看次数

为什么HttpWebResponse会丢失数据?

在另一个问题中,当通过GetResponseStream()从HttpWebResponse读取时,人们得到的数据不完整.

从嵌入式设备读取数据时我也遇到了这个问题,该设备应该向我发送1000个输入的配置,所有32个字节的头部和64个字节*1000都会产生64032个字节的数据.

直接读取响应流只能为我提供前61个半输入的数据,从那里只有零.

版本a)不工作:

int headerSize = 32;
int inputSize = 64;
byte[] buffer = new byte[(inputSize*1000) + headerSize];

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
    if (stream != null)
    {
        stream.Seek(0, SeekOrigin.Begin);
        stream.Read(buffer, 0, buffer.Length);
    }
}

response.Close();
return buffer;
Run Code Online (Sandbox Code Playgroud)

为了使问题可视化,我单独为每个输入配置打印了64个字节.它基本上由40个ascii字符和几个字节组成,表示布尔值和整数值.

版本A)输出:

1/1000 | 46656E7374657220576F686E656E2020202020202020202020202020202020202020202020202020000000000F0EB0AA00008100000001800000100090010020
2/1000 | 42574D20576F686E656E202020202020202020202020202020202020202020202020202020202020000000000F0EB0AA00008100000001800000100091010080
…
61/1000 | 53656E736F72203631202020202020202020202020202020202020202020202020202020202020200000000000000000000010003300000000001000C3010000
62/1000 | 53656E736F7220363220202020202020202020202020202020202020202020200000000000000000000000000000000000000000000000000000000000000000
63/1000 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
…
999/1000 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1000/1000 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)

当我将ResponseStream复制到一个新的MemoryStream时,我可以完全读取所有1000个输入而没有任何损坏的字节.

版本B)完美工作:

(另请参阅/sf/answers/1564823221/,在第一种情况下解决了我的问题)

int headerSize = 32;
int inputSize …
Run Code Online (Sandbox Code Playgroud)

c# stream httpwebresponse

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

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

HttpWebResponse登录

我正在尝试从我的.net应用程序登录Web应用程序,但由于某种原因它无法正常工作.这是登录代码:

<form action="./process-login.php" method="post">
       <table border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td>Username:</td>
          <td><input type="text" size="20" name="username" value=""></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" size="20" name="password" value=""></td>
        </tr>
        <tr>
          <td><input type="submit" name="axn" value=Login></td>
        </tr>
      </table>
</form>
Run Code Online (Sandbox Code Playgroud)

这是我如何从.net做到的:

string userName = "user";
string password = "password";

string postData = "username=" + userName;
            postData += ("&password=" + password);
            postData += ("&axn=Login");

HttpWebRequest loginRequest = (HttpWebRequest)
                WebRequest.Create("http://server.com/process-login.php");

//Added following answer begin
CookieContainer CC = new CookieContainer();
loginRequest.CookieContainer = CC;
//Added following answer end

loginRequest.Method = "POST";
loginRequest.Accept …
Run Code Online (Sandbox Code Playgroud)

c# httpwebrequest httpwebresponse

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

响应未解析的HttpWebRequest和Set-Cookie标头(WP7)

我试图获取标题"Set-Cookie"或访问cookie容器,但Set-Cookie标头不可用.cookie位于响应头中,但它不在客户端请求对象中.我正在ClientHttp使用注册堆栈

bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
Run Code Online (Sandbox Code Playgroud)

这是回复:

HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Connection: keep-alive
Status: 200
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.0.pre4
ETag: "39030a9c5a45a24e485e4d2fb06c6389"
Client-Version: 312, 105, 0, 0
X-Runtime: 44
Content-Length: 1232
Set-Cookie: _CWFServer_session=[This is the session data]; path=/; HttpOnly
Cache-Control: private, max-age=0, must-revalidate
Server: nginx/0.7.67 + Phusion Passenger 3.0.0.pre4 (mod_rails/mod_rack)

<?xml version="1.0" encoding="UTF-8"?>
<user>
...
</user>
Run Code Online (Sandbox Code Playgroud)

我的回调代码包含如下内容:

var webRequest = (HttpWebRequest)result.AsyncState;
raw = webRequest.EndGetResponse(result) as HttpWebResponse;
foreach (Cookie c in webRequest.CookieContainer.GetCookies(webRequest.RequestUri))
{
    Console.WriteLine("Cookie['" + c.Name + …
Run Code Online (Sandbox Code Playgroud)

c# cookies httpwebrequest httpwebresponse windows-phone-7

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

如何从受保护的网站登录,导航和返回数据,到目前为止我所做的一切都无法正常工作

虽然我发现了许多关于如何使用HttpWebRequest和Response进行GET和POST的文章和其他信息,但我发现自己很难让事情发挥作用,就像我期望它们一样.

我一直在玩我发现的几个想法,但到目前为止,没有任何工作......我会发布我的代码:

private void start_post()
    {
        string username = txtUser.Text;
        string password = txtPassword.Text;
        string strResponce;
        byte[] buffer = Encoding.ASCII.GetBytes("username="+username+"&password="+password);
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(txtLink.Text);
        WebReq.Method = "POST";
        //WebReq.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
        WebReq.Headers.Add("Translate", "F");
        WebReq.AllowAutoRedirect = true;
        WebReq.CookieContainer = cookieJar;
        WebReq.KeepAlive = true;
        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = buffer.Length;
        Stream PostData = WebReq.GetRequestStream();
        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();

        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
        //txtResult.Text …
Run Code Online (Sandbox Code Playgroud)

c# screen-scraping login httpwebrequest httpwebresponse

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

400 错误请求 | 从 ASP.NET Web API 接收自定义错误消息或模型

我们有一个由第三方公开的 Web API,我们对此没有任何控制权。看起来他们正在包装自定义错误消息模型以返回 400(错误请求))。

\n\n

使用 Postman 时我们得到自定义错误消息模型

\n\n
{\n    "Message":\xc2\xa0"Site Name Exists",\n    "ErrorId":\xc2\xa0"<some-guid>",\n    "ErrorCode":\xc2\xa0400,\n    "GeneratedTime":\xc2\xa0"<some-datatime>",\n    "RequestedUri":\xc2\xa0"origin-api-uri"\n} \n
Run Code Online (Sandbox Code Playgroud)\n\n

我们尝试使用HttpWebResponseWebResponse从 API 获取响应,但它在 上抛出异常并显示 400 代码httpWebRequest.GetResponse()。它没有给我们预期的响应,就像我们在 Postman 中得到的那样,但它说 400(错误请求)并带有默认错误消息“远程服务器返回错误”。

\n\n

我们希望以邮差身份获取原始错误消息。有什么想法吗?

\n

c# httpwebrequest httpwebresponse asp.net-web-api

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