Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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# 在GetStringAsync中处理http响应代码_C#_Windows Phone 8.1 - Fatal编程技术网

C# 在GetStringAsync中处理http响应代码

C# 在GetStringAsync中处理http响应代码,c#,windows-phone-8.1,C#,Windows Phone 8.1,我对C#非常陌生,更不用说Windows Phone开发了:) 我试图发送一个请求,获取JSON响应,但是如果出现错误(比如401),请能够告诉用户这样的错误。这是我的密码: async Task<string> AccessTheWebAsync() { //builds the credentials for the web request var credentials = new NetworkCredentia

我对C#非常陌生,更不用说Windows Phone开发了:)

我试图发送一个请求,获取JSON响应,但是如果出现错误(比如401),请能够告诉用户这样的错误。这是我的密码:

async Task<string> AccessTheWebAsync()
        {
            //builds the credentials for the web request
            var credentials = new NetworkCredential(globalvars.username, globalvars.password);
            var handler = new HttpClientHandler { Credentials = credentials };

            //calls the web request, stores and returns JSON content
            HttpClient client = new HttpClient(handler);
            Task<string> getStringTask = client.GetStringAsync("https://www.bla.com/content");

            String urlContents = await getStringTask;

            return urlContents;

        }
async任务访问webasync()
{
//为web请求生成凭据
var凭证=新的网络凭证(globalvars.username、globalvars.password);
var handler=newhttpclienthandler{Credentials=Credentials};
//调用web请求,存储并返回JSON内容
HttpClient=新的HttpClient(处理程序);
任务getStringTask=client.GetStringAsync(“https://www.bla.com/content");
字符串urlContents=等待getStringTask;
返回内容;
}
我知道这一定是我在发送请求和存储响应的方式上做错了什么…但我不确定是什么

如果出现错误,我会得到一个常规值: net\u http\u消息\u未\u成功\u状态代码


谢谢大家!

不使用HttpClient,而是使用一个普通的好的旧HttpWebRequest:)

async任务访问webasync()
{
HttpWebRequest req=WebRequest.CreateHttp(“http://example.com/nodocument.html");
req.Method=“GET”;
请求超时=10000;
req.KeepAlive=true;
字符串内容=null;
HttpStatusCode=HttpStatusCode.OK;
尝试
{
使用(HttpWebResponse=(HttpWebResponse)wait req.GetResponseAsync())
{
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
content=wait sr.ReadToEndAsync();
代码=response.StatusCode;
}
}
捕获(WebException ex)
{
使用(HttpWebResponse=(HttpWebResponse)ex.response)
{
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
content=sr.ReadToEnd();
代码=response.StatusCode;
}
}
//现在您有了内容和代码。
返回内容;
}
您可以使用te GetAsync()方法而不是GetStringAsync()


通过这种方式,您可以使用HttpStatusCode enumerable检查返回的状态代码。

如果您想要更好地控制,请使用HttpWebRequest。使用它,您可以获得有关错误的所有信息,甚至可以读取错误响应正文。对不起,您可以给我一个示例,说明如何使用它来代替上面的内容吗?我确实尝试过,但我现在遇到的问题是,GetResponse()在WinPhone上似乎不可用。。。有getResponseAsync(),但仍在尝试如何使用它。先生,您真是太棒了。多谢各位@古斯曼喜欢它。无法计算次数,我将HttpClient换成了GOF HttpWebRequest。它不响应HttpHandler,而是规定了一种与请求的应答不同的解决方案如果要在同一行代码中使用
同步等待结果,调用
GetAsync
有什么意义?提交了一份编辑报告。
    async Task<string> AccessTheWebAsync()
    {

        HttpWebRequest req = WebRequest.CreateHttp("http://example.com/nodocument.html");
        req.Method = "GET";
        req.Timeout = 10000;
        req.KeepAlive = true;

        string content = null;
        HttpStatusCode code = HttpStatusCode.OK;

        try
        {
            using (HttpWebResponse response = (HttpWebResponse)await req.GetResponseAsync())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    content = await sr.ReadToEndAsync();

                code = response.StatusCode;
            }
        }
        catch (WebException ex)
        {

            using (HttpWebResponse response = (HttpWebResponse)ex.Response)
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    content = sr.ReadToEnd();

                code = response.StatusCode;
            }

        }

        //Here you have now content and code.

        return content;

    }
HttpResponseMessage response = await client.GetAsync("https://www.bla.com/content");

if(!response.IsSuccessStatusCode)
{
     if (response.StatusCode == HttpStatusCode.Unauthorized)
     {
         do something...
     }
}
String urlContents = await response.Content.ReadAsStringAsync();