首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.Net对象未随请求一起发布

.Net对象未随请求一起发布
EN

Stack Overflow用户
提问于 2016-05-18 21:05:30
回答 1查看 48关注 0票数 0

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

代码语言:javascript
复制
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();
}

EN

回答 1

Stack Overflow用户

发布于 2016-05-18 21:49:41

您面临的问题通常与WebMessageBodyStyle相关。

确保您的接口定义了消息是包装的还是裸露的:

代码语言:javascript
复制
[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流量通常很有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37300521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档