5

Im trying to send a get request and get the content of a webpage. i have these code.

string url = "www.google.com";

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 HttpWebResponse response = (HttpWebResponse)response.GetResponse();

I found these codes from many site and it is correct but "GetResponse" is giving error. Its explanation below.

Is it a problem of Visual Studio 2012 ? It cant find GetResponse method and just find GetResponseStream method when I press G.

I tried to do this with WebClient class but WebClient cannot be found also. Do these problems occur because of Visual Studio 2012? or any reason ?

Error 1 'System.Net.HttpWebResponse' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebResponse' could be found (are you missing a using directive or an assembly reference?) C:\Users\abidinberkay1\documents\visual studio 2012\Projects\App1\App1\BlankPage1.xaml.cs 45 66 App1

Rob
  • 3,276
  • 2
  • 22
  • 25
abidinberkay
  • 1,857
  • 4
  • 36
  • 68
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 04 '12 at 03:14

1 Answers1

4

You're calling "response.GetResponse()." It's request.GetResponse().

Update: Based on your comments, I'll propose some new code:

private async void btnRequest_Click(object sender, EventArgs e)  // Note: async added
{
    string url = "www.google.com";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync(); // (note: await added, method call changed to GetResponseAsync()

}

I've updated your code to use the C# 5.0 async and await pattern. This allows you to easily use asynchronous methods while writing code that feels synchronous. In order to do this, I've added the async keyword to the method declaration, prior to the return type (void in this case), and I've added the await keyword prior to calling WebRequest.GetResponseAsync().

To answer your question regarding how to know which library you're using: in this case, you chose a Windows Store app. You should specifically call out what kind of project you're working on - it'll help us nail these kinds of things down faster.

Rob
  • 3,276
  • 2
  • 22
  • 25
  • thank you for prompt reply but still doesn't work. There is no such class GetResponse for request. When I pressed G again just beginGetResponse, endGetResponse, GetResponseAsync exist. – abidinberkay Dec 04 '12 at 10:11
  • There is in fact such a method: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx . However, if you're using a different base class library, you'll have to say so. Are you using Silverlight, perchance, since you are using XAML? – Rob Dec 04 '12 at 10:13
  • Is there any way of learning which library I am using ? I installed Visual Studio 2012 but did not look libraries detaily. I am trying to make a program for Microsoft Store Application, I chose "Microsoft Store Application - Blank Project" but I think there is a problem about the library as you said. Should I use silverlight ? – abidinberkay Dec 04 '12 at 10:19
  • @Rob I'm trying to do the same but I'm trying to retrieve a xml data to use in my universal app (XAML). How can I store the content of this response in xml format? – envyM6 Sep 21 '15 at 19:37