【问题标题】:How do I send xmlstring with post method using webclient in C#? [duplicate]如何在 C# 中使用 webclient 发送带有 post 方法的 xmlstring? [复制]
【发布时间】:2021-05-20 23:15:07
【问题描述】:

我在服务类中创建了一个方法,该方法调用另一个类来调用 webclient 对象的实例。我正在尝试使用 UploadString 执行 Post 操作。但是,当调用该方法时,我得到空引用异常。网络客户端似乎没有执行。这是我的代码结构:

Service.cs

public static class Service
{
    private static readonly WebClientService _webClientService;
    private static async Task<string> ChangePassword(string ip, string pass)
    {
        _webClientService.UpdatePassword(serverIp, pass);
    }
}

WebClientService.cs

public class WebClientService
{
    private static readonly WebClient _client = new();
    private static string _getNewPwdUrl = 
        "https://execute-api.com/default/pwd?={0}/user/{1}";
    private static string _postNewPwdUrl = 
        "http://{0}/bin/link?cmd=chngpwd&oldpwd={1}";

    static WebClientService()
    {
        _client.UseDefaultCredentials = true;
    }
    
    public string UpdatePassword(string serverIp, string pass)
    {
            string result = string.Empty;
            string urlPwd = string.Format(_getNewPwdUrl, "admin", pass);
            string xmlPayload = DownloadXml(urlPwd);
            string updateUrl = string.Format(_postNewPwdUrl, serverIp, pass);
            try
            {
                result = _client.UploadString(updateUrl, xmlPayload);
                   
            }
            catch (Exception ex)
            {
                Ilogger.Error($"UpdatePassword > Error: {ex.Message}, 
                    stackTrace: {ex.StackTrace}");
            }
            return result;
        }

    public string DownloadXml(string address)
    {
        string result = string.Empty;
        try
        {
            result = _client.DownloadString(address);
        }
        catch (Exception ex)
        {
            Ilogger.Error($"DownloadXml > Error: {ex.Message}, 
                stackTrace: {ex.StackTrace}");    
        }
        return result;
        }

    }

xml 有效负载将只包含一些带有值的 xmlstring,api 将自行读取它。

【问题讨论】:

  • 哪一行出现错误,result = _client.UploadString(updateUrl, xmlPayload);
  • 不,在我调用 UpdatePassword 时在 service.cs 本身中。通过设置断点,我意识到它停在那里。那就是我将 ObjectReference 设置为对象实例的地方。
  • 我认为我创建webclient实例的格式不正确。

标签: c# post webclient


【解决方案1】:

空引用异常发生在哪个方法中?

从事情的外观来看,我假设是当您从您在Service.cs 中发布的_webClientService 中调用ChangePassword sins 时,从未分配给并且始终为空,因此您可能希望将其更改为_webClientService = new WebClientService();

如果它不存在,那么我猜它在static WebClientService() 中,为了避免在_client 的分配和_client.UseDefaultCredentials 的设置之间发生竞争,我建议将其更改为:

static WebClientService()
{
    _client = new WebClient()
    {
        UseDefaultCredentials = true
    };
}

【讨论】:

    【解决方案2】:

    您可能忘记实例化_webClientService

    您在这里为其创建了一个字段:

    private static readonly WebClientService _webClientService;
    

    但您似乎从未真正赋予它价值。但随后继续在此处使用 null 字段:

    private static async Task<string> ChangePassword(string ip, string pass)
    {
         _webClientService.UpdatePassword(serverIp, pass);
    }
    

    确保实例化它,以便它可以用于:

    private static readonly WebClientService _webClientService = new WebClientService();
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2013-09-03
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2018-07-12
      相关资源
      最近更新 更多