Need simple example of Httpclient Get call

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 10471 Views - Last Post: 23 August 2012 - 09:04 PM Rate Topic: -----

#1 rfresh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-August 12

Need simple example of Httpclient Get call

Posted 23 August 2012 - 01:29 PM

I'm new to C# programming and am looking for a simple way to use Httpclient to fetch a text file from my website.

I've found some examples but they produce errors because I don't know how to declare them in the Usings section. Experienced C# coders know how to do this but new coders don't, thus I can't run the examples. Then I see many different ways to use Httpclient.

Can someone show me a simple way to use Httpclient and what using declarations I need?

I found this code which looks pretty simple:


var client = new HttpClient();
var response = client.Get("http://example.org");




I want to display the response in a label on my form so I'm going to use this code:

lbl_Results.Text = response;



Will that work on the response object? I tried some example code before and this line wouldn't work because response wasn't a string.

Thanks for any help...

Is This A Good Question/Topic? 0
  • +

Replies To: Need simple example of Httpclient Get call

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 01:49 PM

HttpClient is new in .NET 4.5. Are you using Visual Studio 2012? Because if not, you won't be able to use HttpClient (though I believe there might be a NuGet package with it).

If you are using .NET 4.5, the namespace it is in is System.Net.Http.

http://msdn.microsof...httpclient.aspx

And no, that won't work. If all you want to do is get the response as a string, you shouldn't use Get. You should use GetString, which will return a string.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,652
  • Joined: 05-May 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 02:28 PM

Actually, if you scroll down on the link posted by Curtis Rutland, you'll see sample code.
Was This Post Helpful? 0
  • +
  • -

#4 rfresh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-August 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 02:38 PM

@Curtis

Yes, I am using VS 2012.

I added

using System.Net.Http;


and I added

var client = new HttpClient();
var response = client.Get("http://www.mysite.com/cmds.txt");


But once again, I get an error: HttpClient does not contain a def for Get.
Was This Post Helpful? 0
  • +
  • -

#5 rfresh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-August 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 02:50 PM

I also tried

var response = client.GetString("http://www.mysite.com/cmds.txt");


but it didn't like GetString either.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,652
  • Joined: 05-May 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 02:55 PM

Scroll to the bottom of this page and look at the sample code: http://msdn.microsof...httpclient.aspx
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 03:14 PM

Because HttpClient does not include synchronous versions of those methods. I said GetString, I should have said GetStringAsync.

And I bet that's going to introduce an entire new question: how to use Async methods.
Was This Post Helpful? 0
  • +
  • -

#8 rfresh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-August 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 03:21 PM

I tried using that code but am getting this error:

Error 1 The type or namespace name 'HttpResponseMessage' could not be found (are you missing a using directive or an assembly reference?)

@Curtis

Adding GetStringAsync produces this error

Error 1 A using namespace directive can only be applied to namespaces; 'System.Net.Http.HttpClient' is a type not a namespace
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,652
  • Joined: 05-May 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 03:35 PM

Show us your full code. The using statement should have covered both errors you reported... unless you put the using statement in the wrong place which would explain the second error.
Was This Post Helpful? 0
  • +
  • -

#10 rfresh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-August 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 03:42 PM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http.HttpClient;
using System.Net;

namespace HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmd_Quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cmd_FTP_Check_Click(object sender, EventArgs e)
        {

            var client = new HttpClient();
            var response = client.GetStringAsync("http://www.rcmyad.com/ralphs_pc_cmds.txt");
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,652
  • Joined: 05-May 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 04:27 PM

Line 10 should only be: using System.Net.Http; as noted on post #4.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 04:28 PM

First: using directives are for namespaces, not types. System.Net.Http.HttpClient is a type in the System.Net.Http namespace. Use that.

If that doesn't work, expand the following spoiler tag:

Spoiler


Second: GetStringAsync is an async method, meaning that it returns a Task<string> rather than a string. In .NET 4.5, we have the async and await keywords to deal with async methods.

So, you mark your method as async, and you await the result of the task:

private async void button1_Click(object sender, EventArgs e)
{
    var client = new HttpClient();
    textBox1.Text = await client.GetStringAsync("http://www.google.com/");
}



That allows the HttpClient to perform its Get on a background thread. Once it's done, control will resume. The result of the task will be passed back through the await statement into the TextBox's Text property.

Side note: there's a reason that they didn't include Synchronous methods for the HttpClient. It's because people do things like making large web requests on the GUI thread, causing the applications to lock up until they're done. MS does not like this, because apps that feel bad make Windows itself seem bad. So they're taking the option away from you (not really, since you can still use the old WebClient in Windows Forms apps, but for Metro, you're stuck with async HttpClient methods).
Was This Post Helpful? 0
  • +
  • -

#13 rfresh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-August 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 06:18 PM

OK, I made your suggested changes and we're probably closer now. I checked your spoiler tag and my System.Net.Http was checked.

Still getting this error:

Error 1 'HelloWorld.HttpClient' does not contain a definition for 'GetStringAsync' and no extension method 'GetStringAsync' accepting a first argument of type 'HelloWorld.HttpClient' could be found (are you missing a using directive or an assembly reference?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;

namespace HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmd_Quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private async void cmd_FTP_Check_Click(object sender, EventArgs e)
        {
            var client = new HttpClient();
            string response = await client.GetStringAsync("http://www.rcmyad.com/cmds.txt");
            lbl_FTP_Results.Text = response;
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 06:34 PM

Quote

HelloWorld.HttpClient


This is telling. You have a class or struct named "HttpClient" somewhere in your project, or something like that, because the compiler is trying to use an HttpClient defined within the HelloWorld namespace, rather than the one defined in the System.Net.Http namespace. If you use Resharper or Visual Studio's built in refactoring tools, you might have accidentally created a class with that name. Or perhaps you did it some other way. Delete or rename whatever class you made called HttpClient.

If you can't find where you defined that, put your cursor on the HttpClient word on line 28 and press F12 (which is "Go To Definition"). It should take you right to the file where it's defied in.

Once you remove the naming conflict, it should use the correct HttpClient and your application should work.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,652
  • Joined: 05-May 12

Re: Need simple example of Httpclient Get call

Posted 23 August 2012 - 06:36 PM

Do you by any chance have a class named HttpClient somewhere in your project?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2