0

I made the follow codes to call a web service data output and it worked. The problem is when I activate the api key on the web software and this key is generated, to call the web service and need to make a api client to request this through an httpRequest, but my problem whenever I run it it says "The remote server returned an error: (404) Not Found." do you have any idea? I have my whole code pasted bellow.

Thanks you very much in advance

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



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

        private void Submit(object sender, EventArgs e)
        {
            localhost.Dashboard proxy = new localhost.Dashboard();
            localhost.ProjectMetaData[] pm = proxy.GetAllProjectMetaData();
            const string URL = "http://localhost/myProgram/";
            const string apiKey = "d26b15b5-e336-48de-9142-939c0e639e8f";
            const string Id = "Id";
            const string Pass = "pass";


            System.Net.HttpWebRequest myHttpWReq;
            System.Net.HttpWebResponse myHttpWResp;

            //myHttpWReq.ContentLength = 0;

            // Make a web request to the web service
            myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(BLUEURL + "http://localhost/myProgram/Dashboard.asmx" + Id + Pass + apiKey);
            myHttpWReq.Method = "Get";

            // Get the response of the web service
            myHttpWResp = (System.Net.HttpWebResponse)myHttpWReq.GetResponse();

            if (myHttpWResp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                //Create an XML reader to parse the response
                System.Xml.XmlReader reader = System.Xml.XmlReader.Create(myHttpWResp.GetResponseStream());

            }

            //set an string output to the windows form
            StringBuilder sb = new StringBuilder();

            foreach (localhost.ProjectMetaData value in pm)
            {
                sb.AppendLine(value.ProjectTitle + "    -    "
                    + value.ProjectID + "    -    "
                    + value.PublishStatus );
               // sb.AppendLine("\r\n\t");
            }

            label1.Text = sb.ToString(); 

        }

    }
}
user3790916
  • 51
  • 1
  • 12

1 Answers1

1

It looks like you are most like generating your request URL incorrectly. You have created your as such:

myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(BLUEURL + "http://localhost/myProgram/Dashboard.asmx" + Id + Pass + apiKey);

The URL generated by this code will be:

 http://localhost/myProgram/Dashboard.asmxIdPassd26b15b5-e336-48de-9142-939c0e639e8f

(Note: I can't find the value of the variable BLUEURL in your code sample, but it will prefix the URL you are generating whatever it is. My best guess is that it is blank because you would be otherwise getting a UriFormatException.)

This does not look like the correct URL you are after, hence the 404 error. What you are probably after is:

myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(String.Format("http://localhost/myProgram/Dashboard.asmx?id={0}&pass={1}&apiKey={2}", Id, Pass, apiKey));

This will send the request to the URL

http://localhost/myProgram/Dashboard.asmx

sending your parameters in the querystring.

However this is likely to still give you an error since you are trying to call an asmx web service which is an XML based web service and therefore your request is in the wrong format.

This tutorial will show you how to add the Dashboard.asmx web service as a Service reference to your project. This will auto-generate a proxy class which will wrap all calls to the web service for you into simple function calls.

Have read of these following answers for more help on how to add the reference:

Community
  • 1
  • 1
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
  • Hi Thanks you for your answer. I did the changed that you gave me but now it give me another error The remote server returned an error: (500) Internal Server Error. – user3790916 Sep 18 '14 at 14:25
  • Hi again!,I could finally erase the error message by doing this myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(String.Format("http://localhost/BlueWebService55/Dashboard.asmx?id={0}&pass={1}&apiKey={2}", BlueId, BluePass, apiKey) + GetAllProjectMetaData); I have created a variable called string GetAllProjectMetaData = "";but the problem is doesnt display anything when I call the web service, it seams that the method is not being consume by my client.In the web application thares an option to invoke the GetAllProjectMetaData method but from my application its doesnt doit – user3790916 Sep 18 '14 at 14:49
  • I have updated my answer to include some instructions on the best way to call a asmx web service. If you're still stuck, it might be worth you doing some research on asmx web services. – Adrian Sanguineti Sep 18 '14 at 23:25
  • Hi and thanks again for the answer. According to the tutorial thats is how to add a web reference with already have done in a first place. I was able to consume the web service with no problem. The problem start to happen when in the web service in question I have enable the api key so this enable the security and to call and consume the web service is not going to work in the same way when my client worked without the api key, It only worked with the Proxy and with the foreach loop ao I was displaying all. thanks – user3790916 Sep 20 '14 at 15:16
  • I'm not sure that I follow your issue. It sounds like to me that you have a web service that you can successfully call using the proxy generated class. Are you attempting to implement a security mechanism on your web service by making it require an API key? Or does this security mechanism already exist? Either way it sounds like this issue is going off topic from your original question which I believe I have answered. – Adrian Sanguineti Sep 23 '14 at 13:50
  • hi, yes the security's mechanism already exists. This was enable and it generate the api key that its in my code so my knowing it I was trying to call the web service but its doesn't work. You already answered my question and I don't get the error anymore but I was hopping to get any idea of what else I could do. Thanks again – user3790916 Sep 24 '14 at 08:50