windows-phone-8 – ‘System.Net.HttpWebRequest’不包含’GetResponse’的定义

前端之家收集整理的这篇文章主要介绍了windows-phone-8 – ‘System.Net.HttpWebRequest’不包含’GetResponse’的定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用框架4.5创建一个在Visual Studio 2013中使用restful GET方法方法.这是windows_phone_8(针对Phone OS 8.0)应用程序.这是我的代码.
static string HttpGet(string url)
    {
        HttpWebRequest req = WebRequest.Create(url)
                             as HttpWebRequest;
        string result = null;
        using (HttpWebResponse resp = req.GetResponse()
                                      as HttpWebResponse)
        {
            StreamReader reader =
                new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }
        return result;
    }

但我得到如下构建错误

‘System.Net.HttpWebRequest’ does not contain a definition for
‘GetResponse’ and no extension method ‘GetResponse’ accepting a first
argument of type ‘System.Net.HttpWebRequest’ could be found (are you
missing a using directive or an assembly reference?)

我不知道为什么会发生这种情况,相同的代码在同一环境中与Windows_application一起工作正常.

更新:我也尝试过使用Web客户端方法

WebClient client = new WebClient();

            client.Headers.Add("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            Stream data = client.OpenRead("http://192.168.10.73:8087/cisms/mobilews/login/userNameCheck?userName=supervisor");
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            data.Close();
            reader.Close();

得到另一组错误……

Error 1 ‘System.Net.WebHeaderCollection’ does not contain a definition
for ‘Add’ and no extension method ‘Add’ accepting a first argument of
type ‘System.Net.WebHeaderCollection’ could be found (are you missing
a using directive or an assembly reference?)

Error 2 ‘System.Net.WebClient’ does not contain a definition for
‘OpenRead’ and no extension method ‘OpenRead’ accepting a first
argument of type ‘System.Net.WebClient’ could be found (are you
missing a using directive or an assembly reference?)

Error 3 ‘System.Net.HttpWebRequest’ does not contain a definition for
‘GetResponse’ and no extension method ‘GetResponse’ accepting a first
argument of type ‘System.Net.HttpWebRequest’ could be found (are you
missing a using directive or an assembly reference?)

更新2:

我根据@Gavin的回答更改了代码如下.

static async void HttpGet(string url)
    {
        Uri uri = new Uri(url);
        string result = null;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";
        using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse,request.EndGetResponse,null)))
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            result = reader.ReadToEnd();
        }
    }

但是控件从以下行返回到调用事件

using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse,null)))

任何有关这方面的帮助将不胜感激.

答案:

我更改了代码如下,它现在正在工作..

public async Task<string> httpRequest(string url)
        {
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            string received;

            using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse,null)))
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(responseStream))
                    {

                        received = await sr.ReadToEndAsync();
                    }
                }
            }

            return received;
        }

主叫部分如下……

private async void Button_Click(object sender,RoutedEventArgs e)
        {
            string uriString = "http://192.168.10.73:8087/cisms/mobilews/login/userNameCheck?userName=supervisor";
            var response = await httpRequest(uriString);
        }

更新3:

我在处理POST请求时还有一个问题.我试过的代码如下.

static string HttpPost(string url,string[] paramName,string[] paramVal)
        {
            HttpWebRequest req = WebRequest.Create(new Uri(url))
                                 as HttpWebRequest;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            // Build a string with all the params,properly encoded.
            // We assume that the arrays paramName and paramVal are
            // of equal length:
            StringBuilder paramz = new StringBuilder();
            for (int i = 0; i < paramName.Length; i++)
            {
                paramz.Append(paramName[i]);
                paramz.Append("=");
                paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
                paramz.Append("&");
            }

            // Encode the parameters as form data:
            byte[] formData =
                UTF8Encoding.UTF8.GetBytes(paramz.ToString());
            req.ContentLength = formData.Length;

            // Send the request:
            using (Stream post = req.GetRequestStream())
            {
                post.Write(formData,formData.Length);
            }

            // Pick up the response:
            string result = null;
            using (HttpWebResponse resp = req.GetResponse()
                                          as HttpWebResponse)
            {
                StreamReader reader =
                    new StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }

            return result;
        }

方法在Windows Phone 8应用程序中有两个构建错误

Error 1 ‘System.Net.HttpWebRequest’ does not contain a definition for
‘GetRequestStream’ and no extension method ‘GetRequestStream’
accepting a first argument of type ‘System.Net.HttpWebRequest’ could
be found (are you missing a using directive or an assembly reference?)

Error 2 ‘System.Net.HttpWebRequest’ does not contain a definition for
‘GetResponse’ and no extension method ‘GetResponse’ accepting a first
argument of type ‘System.Net.HttpWebRequest’ could be found (are you
missing a using directive or an assembly reference?)

谢谢
塞巴斯蒂安

WP8支持.NET Framework 4.5的子集.

您可以根据需要调整以下代码变体:

WebRequest request = WebRequest.Create(url);
return Task.Factory.FromAsync(request.BeginGetResponse,result =>
{
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
    ...
}

要么

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse,null)))
{
    ...
}
原文链接:https://www.f2er.com/windows/371712.html

猜你在找的Windows相关文章