You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 3.5桌面应用调用API返回(404)错误如何解决?

问题根因

  • 你的API接口明确标注为GET请求,但调用代码使用了POST方法,请求方法不匹配会触发404错误
  • .NET Framework 3.5中的WebClient.DownloadString不存在接受URL、请求方法、请求数据三个入参的重载,调用写法本身不符合语法规范
  • GET请求的参数需要拼接在URL中(路径段/查询字符串),不能像POST请求一样放到请求体传递
  • 你当前的参数拼接逻辑没有匹配API接口的参数接收规则,无法被接口正确识别
  • C#是大小写敏感语言,你代码中写的client.downloadstring首字母小写,会直接触发编译错误

解决方案

根据你的API路由配置不同,选择对应修改方案即可:

方案1:接口使用默认路由,参数通过查询字符串传递

如果你的API没有配置自定义分段路由,默认通过查询字符串接收key和IP参数,修改调用代码如下:

string key = "65354";
string ip = "19216882";
// 拼接GET请求查询参数,Uri.EscapeDataString用于转义特殊字符避免请求异常
string url = string.Format("http://localhost:58167/api/Project?key={0}&IP={1}", 
    Uri.EscapeDataString(key), 
    Uri.EscapeDataString(ip));
string response;
// WebClient实现了IDisposable接口,用using包裹自动释放资源
using (WebClient client = new WebClient())
{
    client.Encoding = Encoding.UTF8;
    // GET请求直接调用DownloadString传入拼接好的URL即可
    response = client.DownloadString(url);
}

方案2:接口配置了分段路由,参数通过URL路径传递

如果你的API路由模板配置为api/Project/{key}/{IP},需要把参数拼接到URL路径中,修改代码如下:

string key = "65354";
string ip = "19216882";
string url = string.Format("http://localhost:58167/api/Project/{0}/{1}", 
    Uri.EscapeDataString(key), 
    Uri.EscapeDataString(ip));
string response;
using (WebClient client = new WebClient())
{
    client.Encoding = Encoding.UTF8;
    response = client.DownloadString(url);
}

内容的提问来源于stack exchange,提问作者percy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 15:09:03