Problem with slash within a query string

对着背影说爱祢 提交于 2019-12-10 10:49:58

问题


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:


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



回答2:


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




回答3:


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



回答4:


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

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



回答5:


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.




回答6:


?

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




回答7:


Use like this

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



来源:https://stackoverflow.com/questions/1238773/problem-with-slash-within-a-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!