在没有asmx,wsdl或添加Web引用的情况下调用第三方WebService

时间:2014-03-25 11:05:27

标签: c# web-services visual-studio wsdl asmx

我需要测试第三方开发的Web服务。第三方既未提供WSDL也未提供ASMX。他们只提供了Web服务的名称,url以及原始XML请求的示例。我现在正试图拨打电话,但我不知道该怎么做!

我尝试了obtain an wsdl by executing visual studio's wsdl.exe,但我收到的错误是"The HTML document does not contain identifying information about the Web service."

在visual studio上,我尝试添加服务引用,提供Web服务的URL。我得到an error downloading '<url>/_vti_bin/ListData.svc/$metadata'. Metadata contains a reference that cannot be resolved.可能是因为Web服务是ASMX而不是WCF?

我认为下一步是尝试use SOAP但是我使用code from here创建了一个类,它创建了肥皂封套,Web请求,将SOAP信封插入到Web请求中,然后创建了一个主适当的电话。但是,当visual studio在IE11上执行该类时,我会得到一个相当随机的HTTP Error 403.14 - Forbidden, the Web server is configured to not list the contents of this directory

除了所有这些尝试之外...... 在visual studio 2012上调用外部Web服务的正确方法是什么,没有WSDL,ASMX或添加Web引用?


更新

我用@Ian提供的代码创建了一个类。我执行了代码(F5)并获得了HTTP error 403.14 Forbidden,如下所示。

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 个答案:

答案 0 :(得分:2)

我建议启动WebClient并手动发送请求。我在最近以XML作为输入的项目中做了类似的事情。

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

然后我使用界面定义了我的客户端,这里是实现。您可以继续使用HTTPClient,但在我的情况下,由于服务器上的操作速度慢,我不得不增加超时。

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