1

I need to send a string to a website and get back a result.

But for example if i send "hello world" it should be "hello%world" instead of space there should be a %

There should be a way i think to make it automatic so it will know where how and when to put this % when the string have spaces in this location.

For example i have this string which is a site url:

https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=de

There is a %20 between the hello and the world. How can i do it ?

2 Answers 2

5

You could use the ParseQueryString method to build a properly encoded query string:

var values = HttpUtility.ParseQueryString(string.Empty);
values["key"] = "INSERT-YOUR-KEY";
values["q"] = "hello world";
string queryString = values.ToString();
// at this stage queryString="key=INSERT-YOUR-KEY&q=hello+world"
Sign up to request clarification or add additional context in comments.

Comments

2

You can use HttpUtility.UrlEncode:

string s = "Hello World";
string t = HttpUtility.UrlEncode(s);//t becomes "Hello+World"

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.