我在使用ServiceStack進行網絡服務。預計標題是:Consuming Web Service HTTP Post
POST /SeizureWebService/Service.asmx/SeizureAPILogs HTTP/1.1
Host: host.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
jsonRequest=string
我想這個代碼來使用它:
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);
}
}
我無法弄清楚如何序列化的DTO前添加jsonRequest=
。我該怎麼做呢?
解決方案基於Mythz的回答:
新增如何我用Mythz的回答爲那些具有在未來同樣的問題(S)的人 - 享受!
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>();
}
感謝Mythz,我想留清楚,但消費的web請求的複雜性讓我想嘗試和數字它與ServiceStack一起出來。我很高興你有這些擴展,真的很感激。 –