0

I am trying to pass a string into a url in C#:

using (var client = new WebClient()) { var responseStr = client.DownloadString("http://api.openweathermap.org/data/2.5 /weather?q=Groningen,nl&APPID=%207b030ffcc7338cc5f1adc4ca8e6205aa"); } 

I there a way to pass a string variable, instead of ?q=Groningen So I could use a text field to get the weather of a city.

I could not find the answer.

Thank you

2
  • 1
    "...q=" + yourStringVariable + "&APP..." Commented Dec 24, 2014 at 14:30
  • Why don't you use string concatenation? Did you encounter a problem? Commented Dec 24, 2014 at 14:45

3 Answers 3

4

In C# you can use + operator to concatenate strings.

So you can use something like following,

using (var client = new WebClient()) { var responseStr = client.DownloadString("http://api.openweathermap.org/data/2.5 /weather?q="+CHOICE+",nl&APPID=%207b030ffcc7338cc5f1adc4ca8e6205aa"); } 

CHOICE is the variable with your desired location.

More on concatenate : here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It worked. I tried it before. But then the blue lined url was no longer 100%. So I thought it was not going to work. I thought i needed Request.Querie.
3

You could use string concatenation to do so:

var url = "http://.....q="+city+"&....."; var responseStr = client.DownloadString(url); 

where city is the variable that holds the city you want to pass.

Comments

0
Webclient client = new Webclient(); string city = "Lahore"; string appId = "123456789"; string url = "http://api.openweathermap.org/data/2.5/weather?APPID="+appId+"&q="+city+""; var json = client.DownloadString(json); 

Now de-serialize josn response as per your requirement.

Either using any way below

  1. JavaScriptSerializer
  2. JSON.Net library
  3. DataContractJsonSerializer

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.