相关疑难解决方法(0)

如何进行HTTP POST Web请求

如何使用该POST方法发出HTTP请求并发送一些数据?我可以做GET请求,但不知道如何制作POST.

.net c# post httpwebrequest httprequest

1033
推荐指数
10
解决办法
147万
查看次数

使用.NET检查Internet连接的最佳方法是什么?

在.NET中检查Internet连接的最快,最有效的方法是什么?

.net c# internet-connection

224
推荐指数
11
解决办法
21万
查看次数

设置webClient.DownloadFile()的超时

我正在使用webClient.DownloadFile()下载文件,我可以为此设置超时,这样如果它无法访问文件就不会花这么长时间吗?

.net c# webclient download

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

Invoke-WebRequest设置超时

我有一个长期运行的网页,我需要Powershell来打电话.我从任务管理器每晚运行它,具体如下:

powershell -Command "Invoke-WebRequest https://www.example.com/longrunningtask" 
Run Code Online (Sandbox Code Playgroud)

但PowerShell超时发生在网站响应之前.有没有办法将超时设置为Invoke-WebRequest超过标准的60秒?

powershell

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

在.NET中有没有办法继承没有构造函数的类?

我目前正在尝试修改一些HttpWebRequest函数,但我不能通过继承来实现,因为HttpWebRequest没有公共构造函数(除了反序列化构造函数).有没有解决方法来做到这一点?

我的目标是编写类似下面示例的代码,但此类对象必须继承HttpWebRequest属性和方法:

using System;
using System.Net;
using System.Threading;

public class AsyncWebRequest:WebRequest
{
    private readonly AsyncCallback getResponseCallback;
    private readonly Uri uri;
    private volatile int RetriesLeft = 3;
    private volatile WebRequest request;

    public AsyncWebRequest(string uri, AsyncCallback getResponseCallback)
       :this(new Uri(uri), getResponseCallback)
    {
    }

    public AsyncWebRequest(Uri uri, AsyncCallback getResponseCallback):base()
    {
        this.uri = uri;
        this.getResponseCallback = getResponseCallback;
    }

    private IAsyncResult BeginGetResponse()
    {
        request = HttpWebRequest.CreateDefault(uri);
        ((HttpWebRequest)request).ReadWriteTimeout = Timeout;
        var result = request.BeginGetResponse(GetResponseCallback, null);
        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
            GetResponseTimeout, null, Timeout, true);
        return result;
    }

    private void GetResponseTimeout(object state, …
Run Code Online (Sandbox Code Playgroud)

.net c# inheritance httpwebrequest

7
推荐指数
2
解决办法
7463
查看次数

WebClient默认超时?

我从/sf/ask/438378321/看到帖子 ,它说默认超时是100秒.但我看到如何更改.NET WebClient对象的超时的评论说

默认超时为100秒.虽然它似乎运行了30秒. - 卡特12年12月13日16:39

在我的程序中,超时总是大约20秒,是否有人知道原因?

c# asp.net

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

C# - StreamReader.ReadToEnd()非常慢

我正在制作一个Web Crawler,我发现我的一个方法GetHTML非常慢,因为它使用StreamReader从HttpWebResponse对象中获取HTML字符串.

这是方法:

static string GetHTML(string URL)
      {
           HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
           Request.Proxy = null;
           HttpWebResponse Response = ((HttpWebResponse)Request.GetResponse());
           Stream RespStream = Response.GetResponseStream();
           return new StreamReader(RespStream).ReadToEnd(); // Very slow
      }
Run Code Online (Sandbox Code Playgroud)

我用秒表进行了测试,并在YouTube上使用了这种方法.

Time it takes to get an HTTP response: 500 MS

Time it takes to convert the HttpWebResponse object to a string: 550 MS
Run Code Online (Sandbox Code Playgroud)

所以HTTP请求很好,只是ReadToEnd()这么慢.

是否有任何替代ReadToEnd()方法从响应对象中获取HTML字符串?我尝试使用WebClient.DownloadString()方法,但它只是一个使用流的HttpWebRequest的包装器.

编辑:尝试使用套接字,它更快:

static string SocketHTML(string URL)
      {
           string IP = Dns.GetHostAddresses(URL)[0].ToString();
           Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
           s.Connect(new IPEndPoint(IPAddress.Parse(IP), 80));
           s.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\n\r\n")); …
Run Code Online (Sandbox Code Playgroud)

.net performance stream httpwebresponse streamreader

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

如何在 C# 中添加超时并重试 url 调用?

我有一个.tgz文件,我需要下载一个Testing文件夹内的 url 。我可以.tgz使用 .url 从 url 成功下载文件WebClient

下面是我的代码:

private void DownloadTGZFile(string url, string fileToDownload)
{
    using (var client = new WebClient())
    {
        client.DownloadFile(url + fileToDownload, "Testing/configs.tgz");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想看看如何为这个调用添加超时,以便如果 url 在特定时间内没有响应,那么它应该超时但它可以重试 3 次然后放弃。另外我想看看我如何HttpClient在这里使用而不是WebClient考虑它是一个较旧的 BCL 类而不推荐。

c# dotnet-httpclient polly

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