c# – 使用httpclient postAsJsonAsync从WCF服务发出错误请求
发布时间:2020-12-15 22:16:54 所属栏目:百科 来源:网络整理
导读:以下是我的wcf服务. public ApiResponseWrapperTextBlobModel PostText(string sessionId,string profileId,TextBlobModel txtModel) {} 接口部分是这样的 [OperationContract] [WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Json,BodyStyl
以下是我的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,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:logsmessages.svclog" /> </listeners> </source> </sources> </system.diagnostics> </configuration> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |