如何使用该POST方法发出HTTP请求并发送一些数据?我可以做GET请求,但不知道如何制作POST.
在.NET中检查Internet连接的最快,最有效的方法是什么?
我正在使用webClient.DownloadFile()下载文件,我可以为此设置超时,这样如果它无法访问文件就不会花这么长时间吗?
我有一个长期运行的网页,我需要Powershell来打电话.我从任务管理器每晚运行它,具体如下:
powershell -Command "Invoke-WebRequest https://www.example.com/longrunningtask"
Run Code Online (Sandbox Code Playgroud)
但PowerShell超时发生在网站响应之前.有没有办法将超时设置为Invoke-WebRequest超过标准的60秒?
我目前正在尝试修改一些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) 我从/sf/ask/438378321/看到帖子 ,它说默认超时是100秒.但我看到如何更改.NET WebClient对象的超时的评论说
默认超时为100秒.虽然它似乎运行了30秒. - 卡特12年12月13日16:39
在我的程序中,超时总是大约20秒,是否有人知道原因?
我正在制作一个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) 我有一个.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# ×6
.net ×5
asp.net ×1
download ×1
httprequest ×1
inheritance ×1
performance ×1
polly ×1
post ×1
powershell ×1
stream ×1
streamreader ×1
webclient ×1