gpt4 book ai didi

c# - restsharp 不成功的帖子

转载 作者:行者123 更新时间:2023-11-30 22:25:08 25 4
gpt4 key购买 nike

我正在尝试使用 restsharp 使用休息服务 (wcf)

这是我的服务

    [ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "/PEmploy", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
Employee PostGetEmploy(Employee emp);
}

[DataContract]
public class Employee
{
[DataMember]
public int EmpNo { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public string DeptName { get; set; }
}

我是这样调用它的

    var client = new RestClient("http://localhost:14437/Service.svc");
var request = new RestRequest("XmlService/PEmploy", Method.POST);
myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" };
request.AddParameter("Employee", emp);
RestResponse<myRef.Employee> response = (RestResponse<myRef.Employee>)client.Execute<myRef.Employee>(request);

这是我得到的异常

    Exception:Caught: "Data at the root level is invalid. Line 1, position 1." (System.Xml.XmlException)
A System.Xml.XmlException was caught: "Data at the root level is invalid. Line 1, position 1."

我已经尝试序列化,但仍然遇到相同的异常。我做错了什么?

最佳答案

我加了

request.RequestFormat = DataFormat.Json

这就成功了。

完整代码

var client = new RestClient("http://localhost:14437/Service.svc");
var request = new RestRequest("XmlService/PEmploy", Method.POST);
request.RequestFormat = DataFormat.Json;
myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" };
request.AddParameter("Employee", emp);
RestResponse<myRef.Employee> response = (RestResponse<myRef.Employee>)client.Execute<myRef.Employee>(request);

关于c# - restsharp 不成功的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com