简体   繁体   中英

Cannot connect to a local network server in Visual Studio Xamarin Android

I'm using Xamarin in Visual Studio Community ; I'm trying to connect to a remote database without success.

Tried WCF, REST, HttpRequest - none can connect to the remote server/service. In a "New Console Application", however, everything works fine. Also made "new Android project", it cannot connect to the service/server, either.

The following code works in a Console Application but cannot connect in Xamarin Mono (Visual studio):

HttpWebRequest request = HttpWebRequest.Create(string.Format(@"http://192.168.16.100/RestService/RestServiceImpl.svc/json/12"));
request.ContentType = "application/json";
request.Method = "GET";

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    if (response.StatusCode != HttpStatusCode.OK)
    {
        Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
    }

    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        var content = reader.ReadToEnd();
        if (string.IsNullOrWhiteSpace(content))
        {
            Console.Out.WriteLine("Response contained empty body...");
        }
        else
        {
            Console.Out.WriteLine("Response Body: \r\n {0}", content);
        }
    }
}
Console.ReadLine();

Have you tried Windows Communication Foundation (WCF) ?

For me it works great as a selfhosted wcf service. (Make sure that you run the selfhoster as Admin, otherwise it will fail)

You will need to download: SlSvcUtil to create your app client and include it in your xamarin project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM