正如 Julian 所建议的那样,我接受这并没有太大的不同,但仍然面临困难可能是因为客户端暴露的 Web 服务的性质。无论如何,这是我最新的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
sherpa_service.SherpaInstanceType create_instance = new sherpa_service.SherpaInstanceType();
sherpa_service.StringType category = new sherpa_service.StringType();
sherpa_service.StringType assignment_group = new sherpa_service.StringType();
sherpa_service.StringType assignee = new sherpa_service.StringType();
sherpa_service.StringType area = new sherpa_service.StringType();
sherpa_service.StringType sub_area = new sherpa_service.StringType();
sherpa_service.StringType impact = new sherpa_service.StringType();
sherpa_service.StringType urgency = new sherpa_service.StringType();
sherpa_service.StringType contact_number = new sherpa_service.StringType();
sherpa_service.StringType title = new sherpa_service.StringType();
sherpa_service.StringType email_id = new sherpa_service.StringType();
sherpa_service.StringType[] desc = new sherpa_service.StringType[3];
desc[0] = new sherpa_service.StringType();
desc[0].Value = "sample description from .net";
sherpa_service.SherpaInstanceTypeDescription description = new sherpa_service.SherpaInstanceTypeDescription();
description.Description = desc;
category.Value = "Finance / Treasury / Tax Apps";
assignment_group.Value = "Procure to Pay";
area.Value = "Finance Apps";
sub_area.Value = "concur";
assignee.Value = "apadiyar";
email_id.Value = "seethak@non.agilent.com";
contact_number.Value = "9884525412";
create_instance.ContactNumber = contact_number;
create_instance.EXternalEmail = email_id;
create_instance.Category = category;
create_instance.AssignmentGroup = assignment_group;
create_instance.Assignee = assignee;
create_instance.Area = area;
create_instance.Subarea = sub_area;
create_instance.Description = description;
sherpa_service.SherpaKeysType create_key = new sherpa_service.SherpaKeysType();
sherpa_service.SherpaModelType create_model = new sherpa_service.SherpaModelType();
sherpa_service.CreateSherpaRequest1 create_incident = new sherpa_service.CreateSherpaRequest1();
create_model.keys = create_key;
create_model.instance = create_instance;
create_incident.CreateSherpaRequest = new sherpa_service.CreateSherpaRequest();
create_incident.CreateSherpaRequest.model = create_model;
BasicHttpBinding binding = new BasicHttpBinding();
binding.SendTimeout = TimeSpan.FromSeconds(25);
//binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Basic;
EndpointAddress address = new EndpointAddress("http://xxxxxxxxxxxxxxxxx/SM/7/ws");
ChannelFactory<sherpa_service.Sherpa> factory =
new ChannelFactory<sherpa_service.Sherpa>(binding, address);
factory.Credentials.UserName.UserName = "xxx";
factory.Credentials.UserName.Password = "xxx";
sherpa_service.Sherpa proxy = factory.CreateChannel();
sherpa_service.CreateSherpaResponse1 respo = proxy.CreateSherpa(create_incident);
Console.WriteLine(respo);
}
}
}
下面是几乎回到第 1 格的异常!我仍在引用您提供的链接,但我几乎无法弄清楚错误。
System.ServiceModel.Security.MessageSecurityException: 'HTTP 请求未经授权,使用客户端身份验证方案'匿名'。从服务器收到的身份验证标头是“Basic realm="CASM"'。”
WebException:远程服务器返回错误:(401)未经授权。
我还想问一下,当我第一次添加 Web 参考时,.net IDE 本身创建的“sherpa 客户端”类的目的是什么?