Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I am developing a PCL Project in Xamarin .I need to pass the IP Address of the device dynamically in the web api Url. My Code is as below:

In the code behind:

string IpAdd = txtIP.Text;

IPAddress = "http://" + IpAdd + ":8124/api/Items/";

MainViewModel.FinalIPAddress = IPAddress;


In my MainViewModel

public static string FinalIPAddress { get; set; }

public static readonly string WebServiceUrl = System.Net.WebUtility.UrlEncode(FinalIPAddress);

But I am unable to get through this. The below exception gets thrown.

Exception thrown: 'System.InvalidOperationException' in System.Net.Http.Phone.ni.DLL Additional information: An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.

How to go about with this? Any help will be appreciated. Thanx in advance.

What I have tried:

[removed]
Posted
Updated 11-Apr-17 10:10am
v2

I think, you need to use HttpUtility.UrlEncodeUnicode and HttpUtility.UrlDecode

See below links

UrlEncode and UrlDecode
 
Share this answer
 
v3
Comments
Member 12956755 11-Apr-17 14:24pm    
thanx a lot.I am sorry but very new to xamarin. but if am not wrong. if u can see my question I am already using HttpUtility.Urlencode in this line:
public static readonly string WebServiceUrl = System.Net.WebUtility.UrlEncode(FinalIPAddress); pls correct me if am wrong and if yes then can u pls show how to use HttpUtility.UrlEncodeUnicode and HttpUtility.UrlDecode? an example should give me a direction.
Kaushikhalvadiya 11-Apr-17 14:55pm    
You have to create and pass url as below.

System.Net.WebUtility.UrlEncode(FinalIPAddress); //This will convert your url like http%3A%2F%2F125.236.25.23%3A8124%2Fapi%2FItems%2F

and in web api, write a code to decode it as below.

System.Net.WebUtility.UrlDecode(FinalIPAddress); //This will convert your url like http://125.236.25.23:8124/api/Items/

Hope, it will solve your problem.:-)
The problem is that you've declared WebServiceUrl as a field. That means the initializer will run once, before the first access to the class, at which point your FinalIPAddress will be null.

When you update the FinalIPAddress property, it will have no effect on the value of the WebServiceUrl field.

Try changing WebServiceUrl to a property:
C#
public static string WebServiceUrl
{
    get { return System.Net.WebUtility.UrlEncode(FinalIPAddress); }
}

However, if you're trying to call the API using that URL, rather than passing the URL as a parameter to the API, then you'll need to remove the UrlEncode call:
C#
public static string WebServiceUrl
{
    get { return FinalIPAddress; }
}
 
Share this answer
 
Comments
Member 12956755 12-Apr-17 4:46am    
Thank you @Richard Deeming. Your solution worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900