c# – 使用httpclient postAsJsonAsync从WCF服务发出错误请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用httpclient postAsJsonAsync从WCF服务发出错误请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1889字,纯文字阅读大概需要3分钟。
内容图文
![c# – 使用httpclient postAsJsonAsync从WCF服务发出错误请求](/upload/InfoBanner/zyjiaocheng/772/ca4e4883d4e347ec8ed4f50ce12a3084.jpg)
以下是我的wcf服务.
public ApiResponseWrapper<TextBlobModel> PostText(string sessionId, string profileId, TextBlobModel txtModel)
{}
接口部分是这样的
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "session/{sessionId}/profile/{profileId}/text")]
ApiResponseWrapper<TextBlobModel> PostText(string sessionId, string profileId, TextBlobModel txtModel);
而模型是
[DataContract]
public class TextBlobModel
{
[DataMember]
public string text { get; set; }
[DataMember]
public DateTime receivedTime { get; set; }
}
当我通过以下方式调用上述服务时,我总是得到错误请求错误.
var baseApiUrl = "http://localhost:51398/api.svc/";
HttpClient authClient = new HttpClient();
authClient.BaseAddress = new Uri(baseoAuthApiUrl);
apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var txtModel = new TextBlobModel();
txtModel.text = "Hello How are you";
txtModel.receivedTime = DateTime.Now;
HttpResponseMessage txtResponse = apiClient.PostAsJsonAsync(String.Format("session/{0}/profile/{1}/text", "sessionId", "profileId"), txtModel).Result;
var txtData = txtResponse.Content.ReadAsAsync<RootObject>().Result;
见下图.
你能告诉我这里做错了什么吗?
解决方法:
试试这个,改变UriTemplate
"session/{sessionId}/profile/{profileId}/text"
至
"PostText/session/{sessionId}/profile/{profileId}/text".
试试这个’BodyStyle = WebMessageBodyStyle.WrappedRequest’,
并将此添加到配置文件以添加跟踪日志,之后您将收到实际错误.
<configuration>
<system.diagnostics>
<trace autoflush="true">
</trace>
<sources>
<source name="System.ServiceModel" switchValue="Critical,Information,ActivityTracing" propagateActivity="true">
<listeners>
<add name="sdt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\logs\messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
内容总结
以上是互联网集市为您收集整理的c# – 使用httpclient postAsJsonAsync从WCF服务发出错误请求全部内容,希望文章能够帮你解决c# – 使用httpclient postAsJsonAsync从WCF服务发出错误请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。