C# RestSharp不';无法获取响应的数据或内容
我的web服务中有一个路由,它接收带有Json正文的POST请求,并返回Json格式的简单数组。我用邮递员来测试路线,效果非常好。但当我使用RestSharp时,它不会得到任何内容(或反序列化情况下的数据) 这是我的C#代码: 这是邮递员的输出: 我使用的PHP代码是: 函数silverum\u更新用户配置文件($request){ } 当PHP方法返回“Parameter”时,它是OK的,PostMan和RestSharp都可以看到输出内容,但当该方法返回新数组时,只有PostMan能够接收returnd对象。我在这个问题上花了几个小时,但没有取得任何进展。请提供帮助。尝试在RestRequest对象中使用AddJsonBody()方法,而不是手动添加参数C# RestSharp不';无法获取响应的数据或内容,c#,php,json,postman,restsharp,C#,Php,Json,Postman,Restsharp,我的web服务中有一个路由,它接收带有Json正文的POST请求,并返回Json格式的简单数组。我用邮递员来测试路线,效果非常好。但当我使用RestSharp时,它不会得到任何内容(或反序列化情况下的数据) 这是我的C#代码: 这是邮递员的输出: 我使用的PHP代码是: 函数silverum\u更新用户配置文件($request){ } 当PHP方法返回“Parameter”时,它是OK的,PostMan和RestSharp都可以看到输出内容,但当该方法返回新数组时,只有PostMan能够接收r
public static async Task<string> UpdateProfile(Profile user, string serviceUrl)
{
var client = new RestClient(serviceUrl);
var request = new RestRequest();
request.Method = Method.POST;
request.AddJsonBody(user);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var response = await client.ExecuteAsync<Profile>(request);
return response.Data.Address;
}
公共静态异步任务更新配置文件(配置文件用户,字符串serviceUrl)
{
var client=新的RestClient(serviceUrl);
var request=new RestRequest();
request.Method=Method.POST;
request.AddJsonBody(用户);
request.OnBeforeDeserialization=resp=>{resp.ContentType=“application/json”;};
var response=wait client.ExecuteAsync(请求);
返回response.Data.Address;
}
最好的方法是使用像wireshark或fiddler这样的嗅探器。比较好结果和坏结果的第一个请求。使第一个请求的错误结果看起来像是正确的请求。而不是request.AddParameter-尝试使用request.AddJsonBody(用户),那么您也不需要手动序列化它。我已经更新了anwser,我将ExecuteTaskAsync更改为ExecuteAsync(请求),在我的虚拟应用程序中,这可以正常工作。请告知我此更改是否解决了您的问题。如果问题仍然存在,请共享serviceUrl(如果这不是专用的)。如果是,请确保它是指向端点的完整路径,如果不是,并且类似,则需要添加请求。Resource=“function1”谢谢Rayan。问题解决了。由于json反序列化程序和PHP都区分大小写,所以出现了此问题。很高兴您对其进行了排序。:)
public class Profile
{
public string Name { get; set; }
public string Family { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public string Address { get; set; }
public string Postal_code { get; set; }
public string Education { get; set; }
public string Gender { get; set; }
public string Age { get; set; }
public string Default_contact { get; set; }
public override string ToString()
{
return string.Concat(Name," " ,Family, " ", Address);
}
}
{
"Name": "Holma",
"Family": "Kool",
"Email": "dr@gmail.com",
"Mobile": "09063094744",
"Address": "some city- basic av. sq 60",
"Postal_code": "10246666",
"Education": "1",
"Gender": "male",
"Age": "35"
}
$parameters = $request->get_json_params();// this is a WordPress method and works just fine
$name=sanitize_text_field($parameters['name']);
$family=sanitize_text_field($parameters['family']);
$email=sanitize_text_field($parameters['email']);
$mobile=sanitize_text_field($parameters['mobile']);
$address=sanitize_text_field($parameters['address']);
$postal_code=sanitize_text_field($parameters['postal_code']);
$education=sanitize_text_field($parameters['education']);
$gender=sanitize_text_field($parameters['gender']);
$age=sanitize_text_field($parameters['age']);
$extdp = [
"Name"=>$name,
"Family"=>$family,
"Email"=>$email,
"Mobile"=>$mobile,
"Address"=>$address,
"Postal_code"=>$postal_code,
"Education"=>$education,
"Gender"=>$gender,
"Age"=>$age
];
return $extdp;
public static async Task<string> UpdateProfile(Profile user, string serviceUrl)
{
var client = new RestClient(serviceUrl);
var request = new RestRequest();
request.Method = Method.POST;
request.AddJsonBody(user);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var response = await client.ExecuteAsync<Profile>(request);
return response.Data.Address;
}