gpt4 book ai didi

c# - 使用 httpclient postAsJsonAsync 来自 WCF 服务的错​​误请求

转载 作者:行者123 更新时间:2023-11-30 12:43:30 25 4
gpt4 key购买 nike

以下是my wcf服务

  public ApiResponseWrapper<TextBlobModel> PostText(string sessionId, string profileId, TextBlobModel txtModel)
{}

这个的接口(interface)部分是

 [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;

见下图。

enter image description here

你能告诉我这里做错了什么吗?

最佳答案

试试这个,改变 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 服务的错​​误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com