views:

1259

answers:

6

I'm using the WebRequest class to make a request to some site. The query string contains a slash (/), which cause to the url to be cut by the site, because it doesn't see it as part of the query string.

The query string is: "my params / separated by slash".

The request:

var request = WebRequest.Create("http://www.somesime.com/q-my+params+%2f+separated+by+slash");

What I missing?

EDIT: After all answers here are update:

I was wrong about query string, it's not actually query string, but the url should look (without "?"):

"http://www.somesime.com/q-my+params+%2f+separated+by+slash"

The url "http://www.somesime.com/q-my+params+%2f+separated+by+slash" is result of Server.UrlEncode method. The code:

var url = "http://www.somesime.com/q-" + Server.UrlEncode(@"my params / separated by slash");

EDIT 2: If I place the resulting url into a browser, everything works. But if I run it through WebRequest class, the url results as it was called without "/ separated by slash" part

+1  A: 

If this is your actual code you are missing the ?:

var request = WebRequest.Create("http://www.somesime.com/?q=my+params+%2f+separated+by+slash");
veggerby
A: 

UrlEncode it. (You will need a reference to System.Web )

string url = "http://www.somesime.com/?q=my+params+%2f+separated+by+slash");
var request = WebRequest.Create(HttpUtility.UrlEncode(url));
Dan Diplo
You should only url encode the values, not the entire url, and that is already done...
Guffa
Yes, you are correct - only the values should be encoded (presuming they haven't already been).
Dan Diplo
A: 

you forgot to put "?" before key name , so try :

var request = WebRequest.Create("http://www.somesime.com?q=my+params+%2f+separated+by+slash");
Adinochestva
A: 

This part of the URL:

/q=my+params+%2f+separated+by+slash

is actually a continuation of the URL, the website probably uses some kind of URL routing. Query strings are denoted by the '?' and seperated by '&'.

If you did need to remove '/' from a URL then HttpUtility.UrlEncode would be the way to go, but this will not benefit you in your case as any encoding done to the URL will almost definitely cause your WebRequest to fail.

Charlie
A: 

?

(Yes, that is what you are missing. :)

Guffa
A: 

You need to have a look at apaches AllowEncodedSlashes option

http://httpd.apache.org/docs/2.0/mod/core.html#allowencodedslashes

You should be able to enable this through .htaccess or httpd_conf

TaRaKa