[英]Xamarin.forms Mac Connection refused when trying to connect to a localhost server
In my project I have to deserialize a list of Object Products that I have to get from a localhost server.在我的项目中,我必须反序列化必须从本地主机服务器获取的 Object 产品列表。 However every time I try to get that data using HttpClient() I get a connection refused exception, which leads to another exception because now I have a null list.
但是,每次我尝试使用 HttpClient() 获取该数据时,都会出现连接被拒绝异常,这会导致另一个异常,因为现在我有一个 null 列表。
I am able to see the serialize list in my localhost(https://localhost:5002/controller/Getall), I am just trying to deserialize this list from the API to my application.我可以在我的本地主机(https://localhost:5002/controller/Getall)中看到序列化列表,我只是想将此列表从 API 反序列化到我的应用程序。
The message is: Connection refused and the e.getBaseExeption is: Java.Net.ConnectionExecption消息是:连接被拒绝并且 e.getBaseExeption 是:Java.Net.ConnectionExecption
How can I get my connection to actually connect?我怎样才能让我的连接真正连接?
App.xaml.cs in application应用程序中的 App.xaml.cs
public App()
{
InitializeComponent();
var handler = new WebRequestHandler();
try
{
List<Product> test = JsonConvert.DeserializeObject<List<Product>>(handler.Get("https://localhost:5002/controller/GetAll").Result);
foreach (Product x in test)
{
Console.WriteLine(x.Name);
}
}
catch (Exception e)
{
Console.WriteLine("e message: " + e.Message);
}
WebRequestHandler in application应用程序中的 WebRequestHandler
public class WebRequestHandler
{
private HttpClient Client { get; }
public WebRequestHandler()
{
Client = new HttpClient();
}
public async Task<string> Get(string url)
{
try
{
using (var client = new HttpClient())
{
var response = await client.GetStringAsync(url).ConfigureAwait(false);
return response;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + " " + e.GetBaseException());
}
return null;
}
public async Task<string> Post(string url, object obj)
{
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
var json = JsonConvert.SerializeObject(obj);
using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
{
request.Content = stringContent;
using (var response = await client
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
return "ERROR";
}
}
}
}
}
}
InventoryController库存控制器
[ApiController]
[Route("Controller")]
public class InventoryController : ControllerBase
{
[HttpGet("GetAll")]
public ActionResult<List<Product>> Get()
{
return Ok(DataContext.Inventory);
}
}
DataContext.cs数据上下文.cs
public class DataContext
{
public static List<Product> Inventory = new List<Product>
{
new ProductByQuantity(2.00, 20, "Ketchup", "Canned & Packaged Foods", 0002),
new ProductByQuantity(2.00, 35, "Mayonnaise", "Canned & Packaged Foods", 0003),
new ProductByQuantity(5.00, 25, "Chocolate bar", "Canned & Packaged Foods", 0004),
new ProductByQuantity(3.50, 100, "Paper Towels", "Miscellaneous Kitchen Items", 0005),
new ProductByQuantity(2.35, 80, "Plastic Wrap", "Miscellaneous Kitchen Items", 0006),
new ProductByQuantity(1.50, 90, "Yougurt", "Refrigerated Foods", 0007),
new ProductByQuantity(1.25, 150, "Bagels", "Bakery", 0008),
new ProductByQuantity(1.00, 200, "Bread", "Bakery", 0009),
new ProductByQuantity(3.50, 45, "Cereal", "Breakfast", 0010),
};
}
You have to remember that localhost refers to the machine at hand.您必须记住 localhost 指的是手头的机器。 In this case if you say localhost:5002 you are telling the device to go to port 5002 and go to that URL which of course won't exist.
在这种情况下,如果你说 localhost:5002 你告诉设备 go 到端口 5002 和 go 到那个 ZE6B391A8D2C4D45902A23A8B6585703D 当然不会存在。
What you need is some sort of tunnel like ngrok ( https://ngrok.com/ ).您需要的是某种隧道,例如 ngrok ( https://ngrok.com/ )。 This way instead of calling localhost you will instead pass your ngrok url that runs on the machine that hosts the API and this way it will make that localhost API visible to the outside world.
这种方式而不是调用 localhost 您将传递您的 ngrok url,它在托管 API 的机器上运行,这样它将使 localhost API 对外界可见。
However, if it is an Android (I ask because I see your exception mentioned Java) if you use 10.0.2.2
(If that one doesn't work try 0.0.0.0
) that is the local callback it has and it should be able to connect to your localhost if you use that IP.但是,如果它是 Android (我问是因为我看到您提到 Java 的异常),如果您使用
10.0.2.2
(如果那个不起作用,请尝试0.0.0.0
)那是它拥有的本地回调,它应该能够如果您使用 IP,请连接到您的本地主机。
Again though, the preferred approach would be some sort of tunnel同样,首选方法是某种隧道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.