[ad_1]
我有一个调用 API 的函数,如下所示
现在,我如何从 wsdlUri 获取我传递的信息,例如 userId 和 memberId
。网
public SyncService1() { //InitializeComponent(); siteInfo.AdminLogin = ConfigurationManager.AppSettings["adminID"].ToString(); siteInfo.AdminPassword = ConfigurationManager.AppSettings["adminPWD"].ToString(); siteInfo.wsdlUri = ConfigurationManager.AppSettings["uri"].ToString(); siteInfo.LoggingDirectory = ConfigurationManager.AppSettings["LoggingDirectory"].ToString(); log.Info($"Logging directory is located in {siteInfo.LoggingDirectory}"); Console.WriteLine($"Logging directory is located in {siteInfo.LoggingDirectory}"); // Initialize log4net. log4net.Config.XmlConfigurator.Configure(); }
我期待着
siteInfo.wsdlUri.Userid=`您的文本`
siteInfo.wsdlUri.MemeberId=`您的文本`
我尝试过的:
siteInfo.wsdlUri.Userid=`your text` siteInfo.wsdlUri.MemeberId=`your text`
解决方案1
您可以使用 System.Uri
类来解析 URI 并提取所需的信息。
C#
siteInfo.wsdlUri = ConfigurationManager.AppSettings["uri"].ToString(); string uri = siteInfo.wsdlUri; System.Uri parsedUri; // To validate the URI format if (Uri.TryCreate(uri, UriKind.Absolute, out parsedUri)) { // Here you can use it from query parameters siteInfo.wsdlUri.UserId = parsedUri.QueryParameters["UserId"]; siteInfo.wsdlUri.MemberId = parsedUri.QueryParameters["MemberId"]; } else { Console.WriteLine("Invalid URI format"); }
如果 URI 不包含查询参数,并且信息以不同的方式编码,则需要相应地调整解析逻辑。
[ad_2]
コメント