2

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

3
  • 1
    in your example, there is no querystring. The querystring is the part of the url after a questionmark. Although that might look a bit like a querystring it is, in fact, part of the path. Commented Aug 6, 2009 at 13:14
  • Server.UrlEncode will remove the '/'s so won't that mess up your webrequest, assuming that it does use URL routing you just altered the path. Commented Aug 6, 2009 at 13:24
  • @F5ToDebug: Server.UrlEncode converts '/' to '%2f'. But the site, I make a request, still see it as '/' Commented Aug 6, 2009 at 13:36

7 Answers 7

1

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");
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

1

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

Comments

0

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));

2 Comments

You should only url encode the values, not the entire url, and that is already done...
Yes, you are correct - only the values should be encoded (presuming they haven't already been).
0

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.

Comments

0

?

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

Comments

0

Use like this

$qrypic = 'INSERT INTO tbl_propics (userID,num,imagename,propic) VALUES ("$id","1","http://\graph.facebook.com/\$id/\picture?type=large","1")';

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.