【问题标题】:Calling 3rd party WebService without asmx, wsdl, or adding web reference在没有 asmx、wsdl 或添加 Web 引用的情况下调用 3rd 方 WebService
【发布时间】:2014-05-03 04:22:39
【问题描述】:

我需要测试第三方开发的网络服务。第三方既没有提供 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 创建了一个类,该类创建soap 信封、Web 请求、将​​soap 信封插入到Web 请求中,然后是一个进行适当调用的main。但是,当 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", ""));
    }        
}

【问题讨论】:

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


    【解决方案1】:

    我建议启动 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;
            }
        }
    }
    

    【讨论】:

    • 我试过你的答案,但没有运气。我用结果错误编辑了我的问题。
    • @DiAlex:好的,我认为此时您需要考虑将凭据嵌入到请求中,具体取决于您执行身份验证的方式。目前它告诉您您无权执行该请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多