Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 网络客户端&x2B;UploadStringAsync+;事件:UploadProgressChanged/DownloadProgressChanged_C#_.net_Rest_Httpwebrequest_Webclient - Fatal编程技术网

C# 网络客户端&x2B;UploadStringAsync+;事件:UploadProgressChanged/DownloadProgressChanged

C# 网络客户端&x2B;UploadStringAsync+;事件:UploadProgressChanged/DownloadProgressChanged,c#,.net,rest,httpwebrequest,webclient,C#,.net,Rest,Httpwebrequest,Webclient,我需要和一个休息服务联系 出于某些原因,我必须使用POST请求从该服务获取JSON数据。(我认为它应该使用GET请求,但这不在我的控制之下,我无法更改它…) 一个请求的响应大约为25-30MB。 由于响应很大,我决定使用WebClient和UploadStringAsync方法 private string jsonResult = ""; void test() { string credentials = Convert.ToBase64String(Encoding.ASCII.

我需要和一个休息服务联系

出于某些原因,我必须使用POST请求从该服务获取JSON数据。(我认为它应该使用GET请求,但这不在我的控制之下,我无法更改它…)

一个请求的响应大约为25-30MB。 由于响应很大,我决定使用WebClient和UploadStringAsync方法

private string jsonResult = "";

void test()
{
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + passowrd));  
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressHandler);
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompletedHandler);                
        client.Headers.Add("Authorization", "Basic " + credentials);
        client.Encoding = System.Text.Encoding.UTF8;
        Uri uri = new Uri(https://demoapi.org/stuff/prod/0);

        client.UploadStringAsync(uri, "POST", "");
    }
}

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("DOWNLOAD_PROGRESS!");
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}

private static void client_UploadProgressHandler(object sender, UploadProgressChangedEventArgs e)
{
    Console.WriteLine("UPLOAD_PROGRESS!");
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete, {3} of {4} bytes. {5} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage);
}

private void client_UploadStringCompletedHandler(Object sender, UploadStringCompletedEventArgs e)
{
    Console.WriteLine("UPLOAD_COMPLETED!");
    jsonResult = e.Result;
}
使用上面的代码,控制台会多次显示消息UPLOAD_PROGRESS!一旦信息上传完成!但是,我没有看到消息下载进度

UPLOAD_PROGRESS!
UPLOAD_PROGRESS!
....    
UPLOAD_PROGRESS!
UPLOAD_COMPLETED!
以下是我的问题:

1) 我是否可以(真的)使用webclient.UploadStringAsync方法订阅“UploadProgressChanged”事件

Microsoft文档没有说明UploadProgressChanged可用于UploadStringAsync,但无论如何,它似乎与上面的代码一起工作

我还查看了Microsoft referencesource,但我不完全理解它。 UploadStringAsync方法中似乎不存在委托“UploadProgressChanged”

private string jsonResult = "";

void test()
{
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + passowrd));  
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressHandler);
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompletedHandler);                
        client.Headers.Add("Authorization", "Basic " + credentials);
        client.Encoding = System.Text.Encoding.UTF8;
        Uri uri = new Uri(https://demoapi.org/stuff/prod/0);

        client.UploadStringAsync(uri, "POST", "");
    }
}

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("DOWNLOAD_PROGRESS!");
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}

private static void client_UploadProgressHandler(object sender, UploadProgressChangedEventArgs e)
{
    Console.WriteLine("UPLOAD_PROGRESS!");
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete, {3} of {4} bytes. {5} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage);
}

private void client_UploadStringCompletedHandler(Object sender, UploadStringCompletedEventArgs e)
{
    Console.WriteLine("UPLOAD_COMPLETED!");
    jsonResult = e.Result;
}

因此,我想知道在webclient.UploadStringAsync中使用“UploadProgressChanged”是否正确

2) 我可以用webclient.UploadStringAsync方法订阅“DownloadProgressChanged”事件吗

3) 据我所知,webclient在后台使用WebRequest和WebResponse。是否可以获得两个请求的进度

我这样问是因为有时候请求的主体很大,有时候响应很大

4) 如果下载的ProgressChangedEvent不可用,则 如何报告“UploadStringAsync”POST请求的WebResponse部分的进度


谢谢

您上面的代码进行了上载,因此无法接收事件

规则是所有下载或上载异步方法引发相应的下载或上载进度更改事件。在场景下,UploadStringAsync使用的代码与UploadDataAsync使用的代码相同(因为字符串是以字符编码为模的字节数组)

非异步方法不会引发进度事件

如果希望访问底层WebRequest,只需创建一个派生自WebClient的类并重写该方法,如下所示:

public class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        // do something with the request
        // BTW, this is how you can change timeouts or use cookies
        return request;
    }
}

注意:您也可以使用更现代的类(完全跨平台、框架等)而不是WebClient。

上面的代码进行上载,因此无法接收DownloadProgressChanged事件。规则是所有下载或上载异步方法引发相应的下载或上载进度更改事件。非异步方法不会引发进度事件。如果您想要访问底层WebRequest,只需创建一个派生自WebClient的类,并重写受保护的虚拟GetWebRequest(Uri地址)方法。好的,我知道上载方法仅引发上载事件。但是,文档中没有提到UploadStringAsync的ProgressChanged。我可以确定UploadStringAsync调用了进度更改吗?我在referencesource代码中也没有看到委托/回调。它似乎在使用上面的代码,但我不明白为什么文档和代码没有提到或使用它。。附言:你能把答案贴在下面吗?我会给你奖励分数。