0

I am trying to fetch an API REST response but the API's URL has an "?" in the URL (see example below).

HttpWebRequest request = WebRequest.Create("http://api.mydomain.com/news/?tag=sports") as HttpWebRequest;

Is there a way to escape this?

I tried Uri.EscapeUriString and HttpUtility.HtmlEncode but that is not working either.

Any ideas?

4
  • 2
    It is a valid url. Why do you want to escape it? Commented Mar 22, 2012 at 22:36
  • It seems like the "?" is breaking the API's URL and the API is responding with the default category. Commented Mar 22, 2012 at 22:37
  • 1
    It seems like the "?" is breaking the API's URL. No. you should read the API's documentation to learn how to get sports category. Commented Mar 22, 2012 at 22:40
  • My dilemma is that using PHP curl() the url above is returning the correct data. But when using WebRequest.Create() with the same url the API is not returning the same data (it is defaulting to the base category). I basically wanted to know if I was doing something wrong, missing something or if this is a problem with .NET's way to send the request. I believe that when I put the "?" inside the WebRequest.Create() the url is being encoded ... therefore the API is not recognizing the parameter and it is returning the default category (which is "all"). Commented Mar 22, 2012 at 22:51

3 Answers 3

1

You don't need to escape anything. The ? is what separates the path portion of the url from the query string portion. http://api.mydomain.com/news/?tag=sports is a perfectly valid url.

Or maybe your API expects: http://api.mydomain.com/news/sports? Difficult to say without knowing which API you are trying to consume.

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

6 Comments

I understand that the URL is a valid url ... the problem I have is that the url (with the "?") is not working when using HttpWebRequest request = WebRequest.Create();
@user1287083, well, maybe then you should consult the documentation of the API you are trying to consume or contact the authors and ask them what url you should use in order to get news in the sports tag. It is not a question that we may help you with.
If I use curl in PHP and pass along the $api_url = "api.mydomain.com/news/?tag=sports"; it works without a problem.
I will contact the developer who built the API and ask him about it ... maybe he can debug the code and log the get request to see what is being sent when using the WebRequest.Create();.
The url is "api.mydomain.com/news/?tag=sports" ... I am certain ... it was verified using PHP curl. The problem I have is that I now need to create a page in ASP.NET to get the stories that have the "sport" tag. The documentation says that the correct format is "api.mydomain.com/news/?tag=sports" ... so that is not the problem. My issue is that the url is not working when using it inside WebRequest.Create(url-here). I even created a page in PHP to return the json while I resolve this but this is a hack I don't want to use in production.
|
1

First you must create the request with url without params.

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://api.mydomain.com/news/");
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

After this you create a string to post the params.

        strParams = "tag=" + strTag; 
        req.ContentLength = strSaida.Length;

Then write it.

        stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        stOut.Write(strParams);
        stOut.Close();

Is that what you need?

Comments

0

Try passing a Uri object instead of a string to the Create()method.

HttpWebRequest request = WebRequest.Create(new Uri("http://api.mydomain.com/news/?tag=sports")) as HttpWebRequest;

How to add query string to httpwebrequest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.