0
votes

I need to test the web services developed by a third-party. The third party provided neither a WSDL nor an ASMX. They just provided the name of the web service, the url, and an example of the raw XML request. I was now attempting to make the calls, but I have no idea how to do so!

I tried to obtain an wsdl by executing visual studio's wsdl.exe but I got an error saying something like "The HTML document does not contain identifying information about the Web service."

On visual studio, I tried to add a Service Reference, providing the web service's url. I got an error downloading '<url>/_vti_bin/ListData.svc/$metadata'. Metadata contains a reference that cannot be resolved. Probably because the web service is an ASMX and not a WCF?

I think the next would be to try use SOAP but I created a class, using the code from here, that creates soaps envelopes, web requests, inserts soap envelope into web request and then a main that make the appropriate calls. However, when visual studio executes that class on IE11 I get a rather random HTTP Error 403.14 - Forbidden, the Web server is configured to not list the contents of this directory.

So apart from all those attempts... what is the right way, on visual studio 2012, to call an external web service, without WSDL, ASMX or adding a web reference?


UPDATE:

I created a class with the code provided by @Ian. I executed the code (F5) and got an HTTP error 403.14 Forbidden, as show below.

public class TestingClassHttp
{
    /// <summary>
    /// An HTTP based Client for sending request
    /// </summary>
    public class HTTPClient : WebClient
    {
        /// <inheritdoc/>
        /// <remarks>Modify the timeout of the web request</remarks>
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            request.Timeout = (int)new TimeSpan(0, 30, 0).TotalMilliseconds;
            return request;
        }

        /// <inheritdoc/>
        public string Request(string endPoint, string content)
        {
            using (WebClient client = new WebClient())
            {
                client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml";
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                String response = client.UploadString(endPoint, "POST", content);
                return response;
            }
        }
    }

    static void Main(string[] args)
    {
        HTTPClient client = new HTTPClient();            
        Console.WriteLine(client.Request("http://.../_ws/products?o=get", ""));
    }        
}

After executing the code above

1

1 Answers

2
votes

I'd suggest firing up a WebClient and manually sending over a request. I've done similar on a recent project that took XML as an input.

   /// <summary>
    /// Gets the raw as a String.
    /// </summary>
    /// <param name="sql">The SQL.</param>
    /// <returns>The response</returns>
    protected String FindRawResponse(String sql)
    {
        // provide the data source and the SQL needed to find the products
        var postData = new { dataSource = this.DataSource, query = sql };

        // Grab the response and wrap as a stream
        String response = this.Client.Request(String.Format("{0}{1}", this.EndPoint, "/GetSql"), GetQueryString(postData));

        return response;
    }

I then defined my Client using an interface, here's the implementation. You can probably just go ahead and use the HTTPClient, but in my case I had to increase the timeout due to some slow operations on the server.

 /// <summary>
/// An HTTP based Client for sending request
/// </summary>
public class HTTPClient : WebClient, IClient
{
    /// <inheritdoc/>
    /// <remarks>Modify the timeout of the web request</remarks>
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        request.Timeout = (int)new TimeSpan(0, 30, 0).TotalMilliseconds;
        return request;
    }

    /// <inheritdoc/>
    public string Request(string endPoint, string content)
    {
        using (WebClient client = new WebClient())
        {
            client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml";
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

            String response = client.UploadString(endPoint, "POST", content);
            return response;
        }
    }
}