'System.Net.HttpWebRequest'不包含'GetResponse'的定义

时间:2014-06-20 07:20:33

标签: windows-phone-8 visual-studio-2013

我正在使用框架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'不包含的定义   '的GetResponse'没有扩展方法' GetResponse'接受第一个   类型的参数' System.Net.HttpWebRequest'可以找到(是你   缺少using指令或程序集引用?)

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

更新:我已尝试使用网络客户端方法

 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();

又出现了一组错误......

  

错误1' System.Net.WebHeaderCollection'不包含定义   为'添加'没有扩展方法'添加'接受第一个论点   输入' System.Net.WebHeaderCollection'可以找到(你错过了吗?   using指令或程序集引用?)

     

错误2' System.Net.WebClient'不包含的定义   '打开读取'没有扩展方法' OpenRead'接受第一个   类型为#System; Net.WebClient'的参数可以找到(是你   缺少using指令或程序集引用?)

     

错误3' System.Net.HttpWebRequest'不包含的定义   '的GetResponse'没有扩展方法' GetResponse'接受第一个   类型的参数' System.Net.HttpWebRequest'可以找到(是你   缺少using指令或程序集引用?)

更新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, request.EndGetResponse, 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, request.EndGetResponse, 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, 0, 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应用程序中存在两个构建错误

  

错误1&#39; System.Net.HttpWebRequest&#39;不包含的定义   &#39; GetRequestStream&#39;没有扩展方法&#39; GetRequestStream&#39;   接受类型为#System; Net.Net.HttpWebRequest&#39;的第一个参数。可以   找到(你错过了使用指令或汇编引用吗?)

     

错误2&#39; System.Net.HttpWebRequest&#39;不包含的定义   &#39;的GetResponse&#39;没有扩展方法&#39; GetResponse&#39;接受第一个   类型的参数&#39; System.Net.HttpWebRequest&#39;可以找到(是你   缺少using指令或程序集引用?)

由于 塞巴斯蒂安

1 个答案:

答案 0 :(得分:2)

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, request.EndGetResponse, null)))
{
    ...
}