skip to Main Content

I am using an API for my desktop application. The application runs on dot.net framework 3.5.

I have an API which runs perfectly, but when i call the API from the desktop application, there is an error:

"The remote server returned an error: (404)"

But my API runs with no problem. I have two parameters, which will passed in the 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.downloadstring(url, "POST", data);
}

How can I solve the problem?

EDIT:
Here is the code that illustrates how the API looks like:

    // 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;
    }

update solved code:

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

2

Answers


  1. Chosen as BEST ANSWER

    thank you all guys. i got my solved and i updated my update code in question. just change uploadstring to downloadstring . and bingo.. it works. –

    update solved code:

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

  2. try add attribute routing to your 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

    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

    public class ViewModel
    {
     public  string Key {get; set;}
      public string IP  {get; set;}
    }
    
     Route[("~/api/Project")]
     public string GetProjects([FromBody]ViewModel model)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search