【发布时间】:2020-08-17 00:58:51
【问题描述】:
我在下面有这个页面,我想将带有 json 的数据发送到我的 PHP 页面以将用户插入 MySQL 数据库。 但连接失败:“System.Net.WebException:'错误:ConnectFailure(连接被拒绝)'” 我在 xaml 中的页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="fullApp.MainPage">
<StackLayout Margin="0,30,0,0" Padding="20">
<Entry x:Name="user" Placeholder="UserName"></Entry>
<Entry x:Name="pass" Placeholder="Password" ></Entry>
<Entry x:Name="phone" Placeholder="Phone Number"></Entry>
<Entry x:Name="gover" Placeholder="Governorate"></Entry>
<Entry x:Name="city" Placeholder="City"></Entry>
<Entry x:Name="street" Placeholder="Street"></Entry>
<Button x:Name="rigister" Clicked="Rigister_Clicked"></Button>
</StackLayout>
</ContentPage>
我的cs页面:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace fullApp
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
pass.IsPassword = true;
}
void Rigister_Clicked(object sender, EventArgs e)
{
PostJson("http://localhost:3308/test/API/rigister_user.php", new users
{
username = user.Text,
password = pass.Text,
PhoneNumber = phone.Text,
Governorate = gover.Text,
City = city.Text,
Street = street.Text
});
void PostJson(string uri, users postParameters)
{
string postData = JsonConvert.SerializeObject(postParameters);
byte[] bytes = Encoding.UTF8.GetBytes(postData);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.ContentType = "text/xml";
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Count());
}
var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}", httpWebResponse.StatusCode);
throw new ApplicationException(message);
}
}
}
}
public class users
{
public string username { get; set; }
public string password { get; set; }
public string PhoneNumber { get; set; }
public string Governorate { get; set; }
public string City { get; set; }
public string Street { get; set; }
}
}
此行的调试停止,我收到错误消息:“System.Net.WebException:'错误:ConnectFailure(连接被拒绝)'”:
using (Stream requestStream = httpWebRequest.GetRequestStream())
登录页面
注册页面
登录或注册后
【问题讨论】:
-
使用 IP 或 FQDN 而不是 localhost。验证您的 Web 服务器是否可以接受远程连接。使用设备/模拟器上的网络浏览器测试连接。
-
@Jason 在 localhost 我使用哪个 ip? 127.0.01还是wifi ip 192.168......?
-
服务器的实际IP地址。 127.0.0.1 和 localhost 一样
-
您是否尝试过使用 PostMan 之类的工具从外部发布到您的 php 页面?如果问题出在您的 C# 代码或 PHP 文件中,您正在尝试消除
-
@sermed 是您设备中的 wifi。去 reddit.com 看看页面是否加载。也尝试使用您的 IP 地址(例如:10.0.2.2)而不是 localhost