Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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# 来自HttpWebRequest的不稳定回答_C#_Json_Windows Phone 7_Networking_Mvvm Light - Fatal编程技术网

C# 来自HttpWebRequest的不稳定回答

C# 来自HttpWebRequest的不稳定回答,c#,json,windows-phone-7,networking,mvvm-light,C#,Json,Windows Phone 7,Networking,Mvvm Light,我在HttpWebRequest周围使用了一个简单的包装器,它在所有情况下都返回一个字符串:如果答案正确,它将返回答案字符串,如果出现问题,它将返回string.Empty public async Task<string> FireAsync(string method, string postData, string url) { httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

我在HttpWebRequest周围使用了一个简单的包装器,它在所有情况下都返回一个字符串:如果答案正确,它将返回答案字符串,如果出现问题,它将返回string.Empty

    public async Task<string> FireAsync(string method, string postData, string url)
    {
        httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.Method = method; // HttpMethod
        httpWebRequest.Accept = "application/json"; // ;odata=verbose

        try
        {
            var response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return String.Empty;
            }

            var responseStream = response.GetResponseStream();
            var postStreamReader = new StreamReader(responseStream);
            string data = await postStreamReader.ReadToEndAsync();

            return data ?? String.Empty; // if null, return empty
        }
        catch (Exception ex)
        {
            return String.Empty;
        }
    }
公共异步任务FireAsync(字符串方法、字符串postData、字符串url) { httpWebRequest=(httpWebRequest)WebRequest.Create(url); httpWebRequest.Method=Method;//HttpMethod httpWebRequest.Accept=“application/json”//;odata=verbose 尝试 { var response=(HttpWebResponse)等待httpWebRequest.GetResponseAsync(); if(response.StatusCode!=HttpStatusCode.OK) { 返回字符串。空; } var responseStream=response.GetResponseStream(); var postStreamReader=新的StreamReader(responseStream); 字符串数据=等待postStreamReader.ReadToEndAsync(); 返回数据??String.Empty;//如果为null,则返回空 } 捕获(例外情况除外) { 返回字符串。空; } } 问题是,有时它返回空字符串。有时,它会在很长一段时间后返回答案。我不记得在使用WebClient时出现过类似的问题


还有其他人有相同的经验吗?

您是否正在关闭您的响应或响应流?请不要忽略例外,它包含您的问题的答案question@adaam陛下不确定,这是否会有帮助,但我将在两者中添加结束语。请看一看,这可能会对您有所帮助。@ArsenMkrt问题是我无法重现错误。。。但你是对的,我会看看异常的内部