首页 > 技术文章 > .net core 调用webService需要用户名密码时

MySmallWorld 2021-06-03 16:10 原文

    HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
            httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;
            httpTransport.MaxReceivedMessageSize = 65536 * 256;
            TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement();
            textMessageEncoding.MessageVersion = MessageVersion.Soap11;


            SynchronizedCollection<BindingElement> coll = new SynchronizedCollection<BindingElement>();
            coll.Add(textMessageEncoding);
            coll.Add(httpTransport);

            System.ServiceModel.Channels.CustomBinding myBinding = new System.ServiceModel.Channels.CustomBinding(coll);
            myBinding.SendTimeout = new TimeSpan(0, 0, 5, 0);
            EndpointAddress address = new EndpointAddress("http://*****/****.wsdl");

            WebService.Client client = new WebService.Client(myBinding, address);          
            client.ClientCredentials.UserName.UserName = "user123456";
            client.ClientCredentials.UserName.Password = "******";

            //PN获取请求参数
            var Model = new Model();

            Task<WebService.Response> responseTask = client.GetInfoAsync(Model);
            WebService.Response response = responseTask.Result;

 

推荐阅读