【问题标题】:Http post xml file to external url then receive responseHttp post xml 文件到外部 url 然后接收响应
【发布时间】:2019-06-11 17:40:00
【问题描述】:

我正在尝试将 HTTP 发布到外部 URL 并发送 XML 文件。我想从我的 MVC 控制器中执行此操作。我可以看到很多关于从 MVC 控制器接收 XML 文件的建议,但看不到任何关于发送的建议。我希望这是一个非常简单的请求,如果有人能指出我正确的方向,我将不胜感激。

我已将我的 xml 文件创建为 XDocument,并希望使用 http post 发送到第 3 方 url

然后我必须从包含不同 xml 文档的 url 接收响应。

  1. 最初发送 XDocument 的命令是什么。
  2. 我是否创建一个具有相同名称但 [HTTPPOST] 过滤器来接收响应的新控制器操作?
  3. 响应参数是否应该接受数据类型 XDocument 来接收返回的 xml 文档?

如果我没有走在正确的轨道上,请告诉我。

非常感谢

【问题讨论】:

    标签: c# asp.net-mvc-5 http-post


    【解决方案1】:
    1. 如果您已经有一个 XDocument - 您可以调用 .ToString(),它会为您提供一个 XML 字符串,用作您的 POST 请求正文。

    2. 您可以使用HttpClient 发出HTTP POST 请求并处理响应(参见示例)。

    3. 不确定您的意思?

    例子改编自https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8:

    XDocument xDocument = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<note>\r\n  <to>Tove</to>\r\n  <from>Jani</from>\r\n  <heading>Reminder</heading>\r\n  <body>Don't forget me this weekend!</body>\r\n</note>");
    
    string xmlRequestBody = xDocument.ToString();
    
    // Create a New HttpClient object and dispose it when done, so the app doesn't leak resources
    using (HttpClient client = new HttpClient())
    {
        // Call asynchronous network methods in a try/catch block to handle exceptions
        try 
        {
            HttpResponseMessage response = await client.PostAsync("your_external_url", new StringContent(xmlRequestBody, Encoding.UTF8, "text/xml")));
    
            response.EnsureSuccessStatusCode();
    
            // responseBody will contain the response XML document (hopefully!)
            string responseBody = await response.Content.ReadAsStringAsync();
    
            // parse the string into an XDocument
            XDocument responseDocument = XDocument.Parse(responseBody);
    
            Console.WriteLine(responseBody);
        }  
        catch (HttpRequestException e)
        {
            Console.WriteLine("\nException Caught!");   
            Console.WriteLine("Message :{0} ",e.Message);
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试在请求正文中发送 xml 内容。

      在 xmlData 参数中传递 XML 字符串

          private object ProcessResponse(string xmlData)
          {
              HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
              httpWebRequest.ContentType = "application/xml";
              httpWebRequest.Method = "POST";
      
              object result = null;
              if (!string.IsNullOrEmpty(xmlData))
              {
                  byte[] data = Encoding.UTF8.GetBytes(xmlData);
      
                  using (var stream = httpWebRequest.GetRequestStream())
                  {
                      stream.Write(data, 0, data.Length);
                  }
              }
      
              HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
      
              using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
              {
                  result = JsonConvert.DeserializeObject(streamReader.ReadToEnd());
              }
              return result;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多