Web Services 在winforms应用程序中通过https调用asp.netwebservice最简单的方法是什么?

iezvtpos  于 2022-11-15  发布在  .NET
关注(0)|答案(2)|浏览(108)

C# 4.7.2 asp.net经典Web服务,通过winforms使用wsdl.exe生成的代理连接到Web服务。
我有Web应用程序成功连接到Web服务,但我有一个用C#/winforms编写的工具,用于测试Web服务是否处于活动状态并在各个级别(dev/prod/etc)工作
通过https调用.asmx Web服务的最简单方法是什么?
这是它现在的样子,但如果放弃代理更容易的话,我也可以接受

AdStudent.ADStudent ws = new AdStudent.ADStudent();
        ws.Url = "https://jcdcadstudent.bob.org/adstudent.asmx";

        string str = ws.GetGuidString("Brown.Eric");

(错误=基础连接已关闭:发送时发生意外错误)
在Web应用程序中,只需更改url即可,但在winform中则不行。

8aqjt8rx

8aqjt8rx1#

我已经在WinForms中做了类似的事情,但是我向API发送请求,而不是MVC Web应用程序,所以我不知道这是否有帮助,但是我将在这里粘贴我的WinForm应用程序向API发送请求的一个方法:

private async void Timer1_Tick(object Sender, EventArgs e)
    {
        var infos = new FirstEntity();
        infos.Num = 3;
        infos.Temp = 21;
        var json = JsonConvert.SerializeObject(infos);
        var data = new StringContent(json, Encoding.UTF8, "application/json");

        var url = "https://localhost:7078/WeatherForecast";
        using var client = new HttpClient();

        var response = await client.PostAsync(url, data);

        var result = await response.Content.ReadAsStringAsync();
    }
9nvpjoqh

9nvpjoqh2#

上面的答案对我不起作用,我最后用肥皂来做,所以

static string GetData2(string url)
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( "text/xml" ) );
            httpClient.DefaultRequestHeaders.Add( "SOAPAction", "http://tempuri.org/GetGuidString" );

                       
            var soapXml= "<?xml version=\"1.0\" encoding=\"utf-8\" ?> " +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                    "<soap:Body>"+
                        "<GetGuidString xmlns=\"http://tempuri.org/\">"+
                            "<adUsername>Brown.Eric</adUsername>" +
                        "</GetGuidString>" +
                    " </soap:Body>" +
               "</soap:Envelope>";

            var response = httpClient.PostAsync( "https://jcdc-aeef.jcdev.org/JCDCADStudent/ADStudent.asmx", new StringContent( soapXml, Encoding.UTF8, "text/xml" ) ).Result;

            var content = response.Content.ReadAsStringAsync().Result;

            return content;
        }

请查看此URL以了解在SOAP上出错的详细信息
C# .netcore 3.1,从winform调用asmx Web服务
希望它能帮助到别人!

相关问题