我与消费 ServiceStack web服务。预计标题是:
POST /SeizureWebService/Service.asmx/SeizureAPILogs HTTP / 1.1 主机:host 内容类型:应用程序/ x-WWW窗体-urlencoded 的Content-Length:长度 jsonRequest =字符串我想使用此代码来使用它:
公共类JsonCustomClient:JsonServiceClient {公众覆盖字符串格式 {得到 {返回X WWW的形式,进行了urlencoded } } 公共覆盖无效SerializeToStream(ServiceStack.ServiceHost.IRequestContext的RequestContext,对象请求,流的System.IO.Stream) {串消息=jsonRequest =;使用(StreamWriter的SW =新的StreamWriter(流Encoding.Unicode)) { sw.Write(消息); } //我得到一个错误,该流不可写,如果我用上面的 base.SerializeToStream(RequestContext的,请求流); } } 公共静态无效JsonSS(LogsDTO日志) {使用(VAR的客户=新JsonCustomClient()) { VAR响应= client.Post< LogsDTOResponse>(URI +/ SeizureAPILogs,日志); } }
我无法弄清楚如何添加 jsonRequest = 序列化的DTO之前。我如何做到这一点。
根据Mythz的回答解决方案:
添加我如何用Mythz的回答对那些具有在未来同样的问题(S)的人 - !享受
公共静态LogsDTOResponse JsonSS(LogsDTO日志) {字符串URL =的String.Format({0} / SeizureAPILogs,URI); JSON字符串= JsonSerializer.SerializeToString(日志); 字符串数据=的String.Format(jsonRequest = {0},JSON); 变种响应= url.PostToUrl(数据,ContentType.FormUrlEncoded,NULL); 返回response.FromJson< LogsDTOResponse>(); }解决方案
这是一个非常奇怪的使用定制服务的客户端发送 X WWW的窗体-urlencoded 的数据,我认为这是一个有点雄心勃勃,试图为ServiceStack的ServiceClients是为了发送/接收相同的内容-类型。其中,作为即使你的类叫做 JsonCustomClient 它不再是一个JSON的客户,因为你已经overrided对应的格式属性
您具有using语句这将关闭底层流很可能使用的StreamWriter 的问题。此外,我希望你调用基方法是,因为你要对电线URL编码+ JSON内容类型的非法搭配错误。
个人而言,我会避开了ServiceClients的,只需使用任何标准的HTTP客户端,例如: ServiceStack有一些扩展到包装制作中使用HTTP调用所需的通常样板的WebRequest .NET,例如:
VAR的json ={0} / SeizureAPILogs的.fmt(URI) .PostToUrl (jsonRequest =串,ContentType.FormUrlEncoded); VAR logsDtoResponse = json.FromJson< LogsDTOResponse>();
I'm consuming a web-service with ServiceStack. The header expected is:
POST /SeizureWebService/Service.asmx/SeizureAPILogs HTTP/1.1 Host: host Content-Type: application/x-www-form-urlencoded Content-Length: length jsonRequest=stringI'm trying to consume it with this code:
public class JsonCustomClient : JsonServiceClient { public override string Format { get { return "x-www-form-urlencoded"; } } public override void SerializeToStream(ServiceStack.ServiceHost.IRequestContext requestContext, object request, System.IO.Stream stream) { string message = "jsonRequest="; using (StreamWriter sw = new StreamWriter(stream, Encoding.Unicode)) { sw.Write(message); } // I get an error that the stream is not writable if I use the above base.SerializeToStream(requestContext, request, stream); } } public static void JsonSS(LogsDTO logs) { using (var client = new JsonCustomClient()) { var response = client.Post<LogsDTOResponse>(URI + "/SeizureAPILogs", logs); } }I can't figure out how to add the jsonRequest= before the serialized DTO. How do I do this?
Solution based on Mythz's answer:
Added how I used Mythz's answer for someone having the same issue(s) in the future - enjoy!
public static LogsDTOResponse JsonSS(LogsDTO logs) { string url = string.Format("{0}/SeizureAPILogs", URI); string json = JsonSerializer.SerializeToString(logs); string data = string.Format("jsonRequest={0}", json); var response = url.PostToUrl(data, ContentType.FormUrlEncoded, null); return response.FromJson<LogsDTOResponse>(); }解决方案
This is a very weird use of a custom service client to send x-www-form-urlencoded data, I think this is a bit ambitious to try as ServiceStack's ServiceClients are meant to send/receive the same Content-type. Where as even though your class is called JsonCustomClient it's no longer a JSON client since you've overrided the the Format property.
The issue your having is likely using the StreamWriter in a using statement which would close the underlying stream. Also I expect you calling the base method to be an error since you're going to have an illegal mix of Url-Encoded + JSON content-type on the wire.
Personally I would steer clear of the ServiceClients and just use any standard HTTP Client, e.g. ServiceStack has some extensions to WebRequest that wraps the usual boilerplate required in making HTTP calls with .NET, e.g:
var json = "{0}/SeizureAPILogs".Fmt(URI) .PostToUrl("jsonRequest=string", ContentType.FormUrlEncoded); var logsDtoResponse = json.FromJson<LogsDTOResponse>();
更多推荐
消费Web服务HTTP POST
发布评论