我已经创建了一个带有webhttp绑定的wcf-rest自托管服务或web api服务,并将证书附加到它以启用https。当我从控制台应用程序执行以下代码时,服务端收到请求,但参数为空。我做错了什么吗??请帮帮忙。


using (var client = new HttpClient())
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("John:Doe")));
Person person = new Person() { FirstName = "Rudri", LastName = "Test" };
HttpContent content = new StringContent(JsonConvert.SerializeObject(person), System.Text.Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://0.0.0.0:8085/WtfService/HelloWorldPostComplex", content);
var statusCode = response.StatusCode;
var responseMessage = response.ReasonPhrase;
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
Console.ReadLine();
}

发布于 2016-05-18 21:49:41
您面临的问题通常与WebMessageBodyStyle相关。
确保您的接口定义了消息是包装的还是裸露的:
[ServiceContract]
public interface IWtfService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
void HelloWorldPostComplex(Person person);
}此外,请注意WebMessageFormat.Json语句。此外,要查看传输的内容,使用Fiddler调试HTTP流量通常很有帮助。
https://stackoverflow.com/questions/37300521
复制相似问题