简体   繁体   English

框架 3.5 桌面应用程序中的“远程服务器返回错误:(404)”

[英]"The remote server returned an error: (404)" in desktop application for framework 3.5

I am using an API for my desktop application.我正在为我的桌面应用程序使用 API。 The application runs on dot.net framework 3.5.该应用程序在 dot.net 框架 3.5 上运行。

I have an API which runs perfectly, but when i call the API from the desktop application, there is an error:我有一个运行完美的 API,但是当我从桌面应用程序调用 API 时,出现错误:

"The remote server returned an error: (404)" “远程服务器返回错误:(404)”

But my API runs with no problem.但是我的 API 运行没有问题。 I have two parameters, which will passed in the URL.我有两个参数,会传入URL。

Here is my Code:这是我的代码:

string url = "http://localhost:58167/api/Project/";
string data = "65354/19216882";
string response;
WebClient client = new WebClient();
{
     client.Encoding = Encoding.UTF8;
     response = client.UploadString(url, "POST", data);
}

How can I solve the problem?我该如何解决这个问题?

EDIT: Here is the code that illustrates how the API looks like:编辑:这是说明 API 的代码:

    // GET api/Project
    public string GetProjects(string key, string IP)
    {
        string sql = "";
        string en = "";
        if (IP == "19216")
            sql = "garbage1";
        if (IP == "19882")
            sql = " garbage2";
        if (IP == "181249")
            sql = " garbage3";
        if (IP == "85206")
            sql = " garbage4";
        if (IP == "87249")
            sql = " garbage5";

        en = CryptorEngine.Encrypt(sql, key);
        return en;
    }

try add attribute routing to your API尝试将属性路由添加到您的 API

 Route[("~/api/Project/{key}/{ip}")]
 public string GetProjects(string key, string IP)

your code你的代码

string url = "http://localhost:58167/api/Project/65354/19216882";
string response;
WebClient client = new WebClient();
{
     client.Encoding = Encoding.UTF8;
     response = client.UploadString(url, "GET");
}

if you don't have control over api如果您无法控制 api

you can use the existing action您可以使用现有的操作

Route[("~/api/Project")]
public string GetProjects(string key, string IP)

code代码

string url = "http://localhost:58167/api/Project?key=65354&IP=19216882";
    
string response;
WebClient client = new WebClient();
{
     client.Encoding = Encoding.UTF8;
     response = client.UploadString(url, "GET");
}

if you still need to use POST you have to fix the acion如果您仍然需要使用 POST,则必须修复 acion

public class ViewModel
{
 public  string Key {get; set;}
  public string IP  {get; set;}
}

 Route[("~/api/Project")]
 public string GetProjects([FromBody]ViewModel model)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 远程服务器返回错误:(404)找不到-HttpWebResponse - The remote server returned an error: (404) Not Found - HttpWebResponse 远程服务器返回404 - Remote server returned 404 HttpWebRequest引发“ 404远程服务器返回错误:(404)未找到。” - HttpWebRequest throws “The remote server returned an error: (404) Not Found.” for 404 远程服务器返回错误:(404)找不到。 在GetResponse()中 - The remote server returned an error: (404) Not Found. In GetResponse() Picasa API返回“远程服务器返回错误:(404)Not Found。” - Picasa API returns “The remote server returned an error: (404) Not Found.” WebDriverManager - '远程服务器返回错误:(404)未找到。' - WebDriverManager - 'The remote server returned an error: (404) Not Found.' BrowserMob 代理 - 远程服务器返回错误:(404) 未找到 - BrowserMob Proxy - The remote server returned an error: (404) Not Found 远程服务器返回错误 - The remote server returned an error 远程服务器返回一个错误:(404) Not Found error for API calls after server publish - The remote server returned an error: (404) Not Found error for API calls after server publish 服务器返回错误:找不到404 - The server returned an error: 404 not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM