问题
I am using xamarin for one of my chat application in college project in which I use URL parameters to send chat messages to API my problem is when there is a space in the message the URL breaks and application crashes. I want the solution in which I can convert those spaces to %20 standards so that API recognizes that it is a space.
回答1:
You should search properly before you ask the question
this solution is not just limited for Xamarin
if this is your api link: "http://yourapi/chat?msg=yourmsg"
and incoming msg is say "Your Msg"
and you are getting "http://yourapi/chat?msg=your msg"
this surely is not going to work
your desired string must be: "http://yourapi/chat?msg=your%20msg" (If your api recognizes this well)
then this is the solution for you
// you Need to add a Reference to the System.Web assembly.
using System.Web;
var etMsg= FindViewById<EditText> (Resource.Id.editText);
string msg =etMsg.Text.ToString ();
string url = "http://yourapi/chat?msg=" + HttpUtility.UrlEncode(msg);
Any Special Characters can be url encoded with this solution
Happy Coding
回答2:
You can use URLencoder or simply use String replace method e.g. "yourParam".replace ("", "%20").
Check outhe below link for more details :
http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html
来源:https://stackoverflow.com/questions/36585305/send-url-parameters-to-api-in-xamarin-api