"System.Net.HttpWebRequest" does not contain a definition for "GetResponse"

I am creating a method for consuming the soothing GET method in Visual studio 2013 with a 4.5 frame. This is windows_phone_8 application (targeting Phone OS 8.0). Here is my code.

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;
    }

But I get a build error as follows

'System.Net.HttpWebRequest' does not contain a definition for "GetResponse" and no extension method "GetResponse" that accepts the first argument of the type "System.Net.HttpWebRequest" can be found (are you missing the using directive or the assembly reference?)

I don’t know why this is happening, the same code works fine with Windows_application in the same environment.

Update: I also tried using the web client method

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

Got another set of errors ...

1 'System.Net.WebHeaderCollection' "" "", type 'System.Net.WebHeaderCollection' ( ?)

2 "System.Net.WebClient" "OpenRead" 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 'System.Net.HttpWebRequest' 'GetRequestStream' 'GetRequestStream' "System.Net.HttpWebRequest" ( ?)

2 'System.Net.HttpWebRequest' "GetResponse" "GetResponse" , "System.Net.HttpWebRequest" ( using ?)

Sebastian

+4
1

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)))
{
    ...
}
+2

Source: https://habr.com/ru/post/1545327/


All Articles