I'm trying to make a .net maui application that can post to a website that I am using XAMPP/Apache to host. When using a try catch block the only error message that I get is connection failed and even checking the error logs within xampp shows that there wasn't an attempt to connect.
I've tried adding/changing things in the .conf files as well as changing the ip address in the url however I've made 0 progress
Below is the code behind of my .net maui app
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace AppSiteTest;
public class ApiService
{
private readonly HttpClient _httpClient;
public ApiService()
{
_httpClient = new HttpClient();
}
//post method used in second clicked event
public async Task<string> SendPostRequestAsync(string url, object data)
{
var jsonData = JsonSerializer.Serialize(data);
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
return null;
}
}
public partial class MainPage : ContentPage
{
private readonly ApiService _apiService;
private int rowCount = 0;
private int columnCount = 0;
private string _photoPath;
public string PhotoPath
{
get => _photoPath;
set
{
_photoPath = value;
OnPropertyChanged(nameof(PhotoPath));
}
}
public MainPage()
{
InitializeComponent();
BindingContext = this;
photo_grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(100) });
_apiService = new ApiService();
}
//ignore this clicked event
public async void Button_Clicked(object sender, EventArgs args)
{
if (MediaPicker.Default.IsCaptureSupported)
{
FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo != null)
{
// save the file into local storage
string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
using Stream sourceStream = await photo.OpenReadAsync();
using FileStream localFileStream = File.OpenWrite(localFilePath);
await sourceStream.CopyToAsync(localFileStream);
if (PhotoPath != null)
{
photo_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(75) });
columnCount++;
var gridPhoto = new Image
{
Source = PhotoPath,
BackgroundColor = Colors.AliceBlue,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
photo_grid.Add(gridPhoto, columnCount, rowCount);
}
PhotoPath = localFilePath;
}
}
}
//attempt to post begins
public async void btn_Post_Clicked(object sender, EventArgs args)
{
upload_status.Text = "Trying to connect";
var url = "http://10.0.2.2/GuppyTown_CISP316/php/submit.php";
var data = new { fname = "John", lname = "Smith", email = "email@gmail.com", phone = "000-000-0000", tip = "From .net Maui" };
upload_status.Text = "data and url loaded";
try
{
var response = await _apiService.SendPostRequestAsync(url, data);
upload_status.Text = response ?? "No response from server";
}
catch (HttpRequestException httpEx)
{
upload_status.Text = $"Request error: {httpEx.Message} {url}";
}
catch (Exception e)
{
upload_status.Text = $"General error: {e.Message}";
}
}
}
I don't know how to mark an answer, but Jason came through for me as it ended up being that I had to add (UsesClearTextTraffic = True) to the MainApplication.cs file found at Platforms>Android while I am debugging and testing. Thank you all!