我需要使用所有标准RESTful方法发送HTTP请求并访问请求正文以便使用它发送/接收JSON.我调查过,
这几乎是完美的,但有些情况下,例如,如果服务器关闭,函数GetResponse可能需要几秒钟才能返回 - 因为它是一个同步方法 - 冻结该时段的应用程序.这个方法的异步版本,BeginGetResponse,似乎不是异步工作(在Unity中),因为它仍然冻结该时期的应用程序.
由于某些原因,只支持POST和GET请求 - 但我还需要PUT和DELETE(标准的RESTful方法),所以我没有进一步深入研究它.
为了运行WebRequest.HttpWebRequest.GetResponse而不冻结应用程序,我研究了使用线程.线程似乎在编辑器中工作(但看起来非常不稳定 - 如果你在应用程序退出时不停止线程,即使你停止它也会永远在编辑器中运行),并且当构建到iOS设备时它会立即崩溃因为我尝试启动一个线程(我忘记写下错误,我现在无法访问它).
荒谬,甚至不会尝试这个.
这个.我想知道他们是如何管理它的.
这是我正在尝试的WebRequest.BeginGetResponse方法的示例,
// The RequestState class passes data across async calls.
public class RequestState
{
const int BufferSize = 1024;
public StringBuilder RequestData;
public byte[] BufferRead;
public WebRequest Request;
public Stream ResponseStream;
// Create Decoder for appropriate enconding type.
public Decoder StreamDecode = Encoding.UTF8.GetDecoder();
public RequestState()
{
BufferRead = new byte[BufferSize];
RequestData = new StringBuilder(String.Empty);
Request = null;
ResponseStream = null;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用从服务器下载一些文件的DownloadFileAsync方法WebClient,我不禁注意到在VS2010中我的代码的非正式测试中,它在启动时会阻塞大约3秒钟,在我看来,首先打败了目的.
以下是相关的代码片段:
WebClient downloader = new WebClient();
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(updateDownloadProgress);
downloader.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(downloadCompleted);
var current_map = map_downloads[0];//string with filename, map_downloads is List<string>
var path = System.IO.Path.GetTempFileName();
downloaded_maps.Add(path);//adding the temp file to a List<string>
downloader.DownloadFileAsync(new Uri(MAP_BASE + current_map), path); //MAP_BASE is a string containing the base url
Run Code Online (Sandbox Code Playgroud)
DownloadFileAsync当应用程序下载~100 MB文件时,我正在使用UI阻止阻止.显然,如果UI在呼叫开始时阻塞了3秒,那么即使不完全,也会稍微减少效用.
我对C#/ .Net相对缺乏经验(大约3 - 4年前我做了一堆.Net 2.0的东西,IIRC,但我现在基本上重新学习它).
我通过使用提到的建议纠正了我的HttpWebRequest初始调用很慢
提到将Proxy设置为null并且事情会加快的建议之一.
我做到了这一点并且有效.
但是当我在一些客户端站点部署它时,我担心这会产生影响....
可能是某些客户端站点已将其域配置为通过代理到达我正在进行HttpWebRequest的服务器.
将Proxy正确设置为null会对此产生影响吗?
谢谢
我正在尝试使用WPF和MVVM从我的网络服务器下载一个大文件(500 mb).因此,以下属性都绑定到某种控件(进度条).问题是,即使使用DownloadFileAsync,应用程序仍然会挂起.
该文件被下载,因为我可以从我的日志(文件越来越多,当然)告诉.
这是我的代码:
#region Methods
private void StartDownload(string url, string localPath)
{
Logger.Debug("Starting to initialize file download");
if (!_webClient.IsBusy)
{
_webClient = new WebClient();
_webClient.Proxy = null; // http://stackoverflow.com/questions/754333/why-is-this-webrequest-code-slow/935728#935728
_webClient.DownloadFileCompleted += webClient_DownloadFileCompleted;
_webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
_webClient.DownloadFileAsync(new Uri(url), localPath);
}
Logger.Debug("Finished initializing file download");
}
private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Logger.Debug("Download finished! Cancelled: {0}, Errors: {1} ", e.Cancelled, e.Error);
}
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Logger.Debug("Downloading... Progress: {0} ({1} bytes / {2} bytes)", e.ProgressPercentage, …Run Code Online (Sandbox Code Playgroud)