C# 使用 RestSharp 发布 XML 文件

标签 c# http-post restsharp rest-client

我已经编写了一种方法来将消息发布到 uri。

public string RestClientPost(string uri, string message = null)
    {
        var client = new RestClient(uri);
        var request = new RestRequest(Method.POST);
        request.AddHeader("Accept", "text/xml");
        if (!string.IsNullOrEmpty(message))
            request.AddParameter(message, ParameterType.RequestBody);

        var result = "";
        var response = client.Execute(request);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            result = response.Content;
            Console.WriteLine(result);
        }
        else
        {
            result = response.StatusCode.ToString();
        }


        return result;
    }

下面的代码是用上面的方法发帖。

public void test123()
    {
        string uri = "myuri"; //private uri, cannot expose.
        var file= System.IO.File.ReadAllText(Path.Combine(Settings.EnvValPath, "RestClientXML", "test.XML"));
        var content = new RestClientServices().RestClientPost(uri, file);

    }

但是,它返回“不支持的媒体类型”。

我的test.XML的内容是

<customer> 
    <customerName>test</customerName > 
    <customerStatus>OK</customerStatus > 
</customer>

并且使用适用于 Google Chrome 的 Advanced Rest Client 插件,我能够发布它并返回我想要的字符串。有什么不对??我在 Advanced Rest Client 中将“content-type”设置为“text/xml”。

  • 返回信息是客户的id。例如:2132

最佳答案

我正在使用 postman ,

postman get the c# code tools

如果您可以使用此工具调用任何 xml 网络服务,那么您可以单击 code并选择 restsharp 并将其复制粘贴到您的代码中

关于C# 使用 RestSharp 发布 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690621/

相关文章:

c# - 使用 Entity Framework 从 WCF RIA 服务调用另一个 WCF 数据服务

android - 将数据从 Android 发送到 Webservice 以插入到我的数据库中

c# - 如何在 Windows Phone 8 中为 RestClient 设置超时?

c# - 使用 RestSharp 客户端反序列化嵌套的 JSON 响应

asp.net-mvc - 如何向 RestSharp 客户端返回 401 身份验证错误?

c# - 使用 open xml sdk 从 xlsx 中读取日期

c# - 如何使用 TypeConverter 获取标志枚举以转换为 UInt64

java - 在 HTML 表单中发送 API key

c# - 为什么我的角色/相机在 Unity 2d 中卡顿?