Send URL parameters to api in xamarin API

跟風遠走 提交于 2019-12-12 21:03:29

问题


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

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