gpt4 book ai didi

c# - 为什么从浏览器调用时 MySQL 结果正常,但从 C# 调用时为空或 NULL?

转载 作者:行者123 更新时间:2023-11-29 02:45:20 25 4
gpt4 key购买 nike

我正在尝试从我的 C# Windows 应用程序向我的网站发出网络请求,但是当仅从 C# 而不是从它按预期工作的网站调用时,所需的结果为空或 null。

在我发出请求之前,我需要从一个登录请求开始,它按预期工作并且确实返回了正确的值。

重要编辑:

我试图将我的 PHP 代码复制粘贴到我的 login.php 文件中,它确实在 C# 中工作并返回正确的计数值。

是不是我的 HttpClient 配置不正确?


我的 PHP 测试代码如下所示:

<?php
session_start();
if(!isset($_SESSION['user'])){ header("Location: index.php"); }

include_once 'dbconnect.php'; //contains $db

$sql = "SELECT * FROM myTable"; //contains two rows
$sql_res = mysqli_query($db, $sql);
$proxyCount = mysqli_num_rows($sql_res);
$echo "Count: ".$proxyCount;
?>

我的 C# 看起来像这样:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.Net.Http;

using ModernHttpClient;
using Newtonsoft.Json;


namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void log(string str)
{
logbox.AppendText(str + Environment.NewLine);
}

private string host = "http://www.mywebsite.com/";
private HttpClient httpClient = new HttpClient(new NativeMessageHandler());
private async Task<string> request(string target, Dictionary<string, string> _parameters)
{
string uri = host + target;

using (HttpResponseMessage response = await httpClient.PostAsync(uri, new FormUrlEncodedContent(_parameters)))
return new StreamReader(await response.Content.ReadAsStreamAsync()).ReadToEnd();

}

private async void button1_Click(object sender, EventArgs e)
{

string loginResp = await request("login.php", new Dictionary<string, string> { { "username", "user" }, { "password", "password" } });
log(loginResp);



}

private async void button2_Click(object sender, EventArgs e)
{
string proxiesResp = await request("proxy/proxy.php", new Dictionary<string, string> { { "getAllProxyRequests", "" } });
//above returns "count: " in C#, but returns "count: 2" in webbrowser
log(proxiesResp);
}
}
}

最佳答案

找到问题了,是人为错误。

我的文件 dbconnect.php 位于 myProblem.php 所在的目录下。

我不得不换行说

include_once 'dbconnect.php';

include_once '../dbconnect.php'; 

关于c# - 为什么从浏览器调用时 MySQL 结果正常,但从 C# 调用时为空或 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com