加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-8 – ‘System.Net.HttpWebRequest’不包含’GetR

发布时间:2020-12-13 20:29:05 所属栏目:Windows 来源:网络整理
导读:我正在使用框架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
我正在使用框架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)))
{
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读