HttpClient SyncPost 返回“Web 服务方法名称无效”

发布于 2025-01-19 19:32:47 字数 2160 浏览 2 评论 0原文

我绝对是Newby来依靠API的内容,并且我尝试将带有XML主体的帖子到服务器上。服务器还可以(我可以为多样化的呼叫获得一些响应),但是当我尝试发表帖子时,我会得到“方法名称无效”。我看不到有什么问题...

这是我的代码(C#)的一部分:

string post_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " +
                   "<mark> " +
                      $"<idMark>{idMark}</idMark> " +
                      $"<idClipLog>{idClipLog}</idClipLog> " +
                      $"<freeDescription>{freeDescription}</freeDescription> " +
                      $"<tcOut>{tcOut}</tcOut> " +
                      $"<tcIn>{tcIn}</tcIn> " +
                       "<clipLogOperator> " +
                          $"<OperatorName>{operatorName}</OperatorName>" +
                       "</clipLogOperator>" +
                       "<clipLogLayer>" +
                          $"<layerName>{layerName}</layerName>" +
                       "</clipLogLayer>" +
                       "<markKeyword>" +
                          $"<idMark>{idMKMark}</idMark>" +
                          $"<idMarkKeyword>{idMarkKeyword}</idMarkKeyword>" +
                          $"<keyword>{keyword}</keyword>" +
                       "</markKeyword>" +
                    "</mark>";


var httpContent = new StringContent(post_body);
var requestUri = $"VideoTags.asmx/CreateMark";
var response = _httpClient.PostAsync(requestUri, httpContent).Result;
if (!response.IsSuccessStatusCode)
{
    var xmldata = response.Content.ReadAsStringAsync();
    var xmlSerializer = new XmlSerializer(typeof(cExtendedSessionsResult));
    using (StringReader reader = new StringReader(xmldata.Result))
    {
        try
        {
            var logIdResp = (cExtendedSessionsResult)xmlSerializer.Deserialize(reader);
        }
        catch
        {
            throw new Exception($"[E04071035] Error in CreateMark [{_server}]");
        }
    }
}

也许是身体内容的问题?我需要将XML传递到邮局中。我从XML创建string -content,然后传递给PostAsync调用。这是正确的吗?

httpclient似乎具有正确的baseaddress(正如我之前所说,我能够称呼一些获取功能)。

提前致谢!

I'm absolute newby to REST API's stuff, and I try to make a POST with a XML body to a server. The server is ok (I can get some responses for a diverse GET calls), but when I try to make a POST I get 'method name is not valid'. I can't see what is wrong...

This is part of my code (C#):

string post_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " +
                   "<mark> " +
                      
quot;<idMark>{idMark}</idMark> " +
                      
quot;<idClipLog>{idClipLog}</idClipLog> " +
                      
quot;<freeDescription>{freeDescription}</freeDescription> " +
                      
quot;<tcOut>{tcOut}</tcOut> " +
                      
quot;<tcIn>{tcIn}</tcIn> " +
                       "<clipLogOperator> " +
                          
quot;<OperatorName>{operatorName}</OperatorName>" +
                       "</clipLogOperator>" +
                       "<clipLogLayer>" +
                          
quot;<layerName>{layerName}</layerName>" +
                       "</clipLogLayer>" +
                       "<markKeyword>" +
                          
quot;<idMark>{idMKMark}</idMark>" +
                          
quot;<idMarkKeyword>{idMarkKeyword}</idMarkKeyword>" +
                          
quot;<keyword>{keyword}</keyword>" +
                       "</markKeyword>" +
                    "</mark>";


var httpContent = new StringContent(post_body);
var requestUri = 
quot;VideoTags.asmx/CreateMark";
var response = _httpClient.PostAsync(requestUri, httpContent).Result;
if (!response.IsSuccessStatusCode)
{
    var xmldata = response.Content.ReadAsStringAsync();
    var xmlSerializer = new XmlSerializer(typeof(cExtendedSessionsResult));
    using (StringReader reader = new StringReader(xmldata.Result))
    {
        try
        {
            var logIdResp = (cExtendedSessionsResult)xmlSerializer.Deserialize(reader);
        }
        catch
        {
            throw new Exception(
quot;[E04071035] Error in CreateMark [{_server}]");
        }
    }
}

Perhaps a problem with the body content? I need pass the XML in the POST body. I create StringContent from my XML and pass to PostAsync call. This is correct?

HttpClient seems have the correct BaseAddress (as I said before, I was able to call some get functions).

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭了丶谁疼 2025-01-26 19:32:48

非常感谢大家!最后我可以与提供商交谈,看来这是 API 接口规范中的错误。

再次感谢您!

Thanks a lot for everybody! Finally I can talk with the provider, and seems that is an error in the specification of the API interface.

Thank you again!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文