![](/img/trans.png)
[英]WebClient SSL Error: "System.Net.WebException: The SLL connection could not be established"
[英]System.Net.WebException: 'Error: ConnectFailure (Connection refused)'
我在下面有这个页面,我想将带有 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())
登录页面
注册页面
登录或注册后
您可以使用 HttpClient 并将其设为异步 function:
async Task<string> PostJson(string uri, users postParameters)
{
string postData = JsonConvert.SerializeObject(postParameters);
using (var client = new HttpClient());
var response = await client.PostAsync(uri, new StringContent(postData));
if (!response.IsSuccessStatusCode)
{
string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
return await response.Content.ReadAsStringAsync();
}
更新:
我知道这并不能解决 OP 的问题,但这只是一种更优雅的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.