system.net.http.httprequestexception:发送请求时发生错误。视觉工作室

时间:2020-08-19 10:20:18

标签: c# mysql visual-studio xamarin mysql-error-1064

我正在使用Visual Studio 2017和MySQL。我最近在计算机硬盘上遇到了问题,因此必须对其进行更改。我又重新下载了Visual Studio和MySQL,我将代码放在笔式驱动器上,因此我将代码重新导入到Visual Studio中并为MySQL重新创建了表。我已将Visual Studio成功连接到MySQL。这是我的代码。

欢迎页面

 public async Task<List<string>> UserNumber()
    {
        MainUserDataRestClient<string> restClient = new MainUserDataRestClient<string>();
        var GetNumber = await restClient.GetAsync();
        return GetNumber;
    }



    protected override async void OnAppearing()
    {
        base.OnAppearing();
        List<string> Number = new List<string>();
        Number = await UserNumber();
        await MainProgressBar.ProgressTo(0.8, 1000, Easing.Linear);

        App.Current.MainPage = new NavigationPage(new HomePage())
        {
            BarBackgroundColor = Color.FromHex("#35ddcf")
        };
        int i = Number.Count;
        if (i == 0)
        {
            await  MainProgressBar.ProgressTo(0.8, 1000, Easing.Linear);
            App.Current.MainPage = new MainPage();
        }
        else
        {
            await MainProgressBar.ProgressTo(0.8, 1000, Easing.Linear);
            App.Current.MainPage = new NavigationPage(new HomePage())
            { BarBackgroundColor = Color.FromHex("#35ddcf") };
        }
    }
}

Restcient

  public class MainUserDataRestClient<T>
{
    private const string WebServiceUrl = "http://localhost:57645/api/MainUserData/";


    public async Task<List<T>> GetAsync()
    {

        var httpClient = new HttpClient();

        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

        return taskModels;
    } }

控制器

 public class MainUserDataController : Controller
{
    // GET: api/MainUserData
    [HttpGet]
    public List<string> Get()
    {
        string sqlstring = "server=localhost; port=3306 ; user id =root;Password=;Database=test;";
        MySqlConnection conn = new MySqlConnection(sqlstring);
        MainPhoneNumber MNP = new MainPhoneNumber();

        MySqlParameter param = new MySqlParameter();
        param.ParameterName = "@id";
        param.Value = MNP.PhoneNumber;


        try
        {
            conn.Open();
        }
        catch (MySqlException ex)
        {
            throw ex;
        }
        string Query = "SELECT * FROM test.maintable where Telephone = @id ";
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        cmd.Parameters.Add(param);
        MySqlDataReader MSQLRD = cmd.ExecuteReader();
        List<string> GetBizList = new List<string>();

        if (MSQLRD.HasRows)
        {

            while (MSQLRD.Read())
            {
                string BV;
                BV = (MSQLRD["Telephone"].ToString());
                GetBizList.Add(BV);
            }
        }

        conn.Close();
        return GetBizList;
    } }

运行代码时,无法从MySQL数据库检索数据,并且出现此异常。

未处理的异常:system.net.http.httprequestexception:发送请求时发生错误

我想念什么?

1 个答案:

答案 0 :(得分:0)

这看起来不像数据库错误。似乎错误来自此行:

var json = await httpClient.GetStringAsync(WebServiceUrl);

关于您的运行环境,我没有太多信息,但是从URL看来,您正在使用iis-Express。首先要看的是URL:

private const string WebServiceUrl = "http://localhost:57645/api/MainUserData/";

您的新设置中的端口是否正确,或者端口已更改并且您尝试访问不存在的URL?

要查看的第二件事是,是否可以在控制器中遇到断点。如果是,您那里是否有任何错误/例外?

如果上述建议不起作用,请添加更多信息,以便我们更好地为您提供帮助。