如何使用JSON请求调用Restful服务c# [英] How to Invoke Restful Service with JSON request c#

查看:141
本文介绍了如何使用JSON请求调用Restful服务c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Json格式的请求调用.net中开发的REST服务。但是,REST服务可以正常处理xml格式的请求。如果下面定义RequestFormat的方式有任何问题,请告诉我。



这是接口代码:

[ServiceContract]

公共接口ISalesOrderPostService

{

// POST操作

[OperationContract]

[WebInvoke(UriTemplate =,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,Method =POST)]

String PostSalesOrder(SalesOrder sales_order);

}



以下是回复:





<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

< html

xmlns =http://www.w3.org/1999/xhtml>

< head>

< title>请求错误< / title>

< style> BODY {color:#000000;背景颜色:白色; font-family:Verdana; margin-left:0px; margin-top:0px; } #content {margin-left:30px; font-size:.70em; padding-bottom:2em; A:link {color:#336699; font-weight:bold;文字装饰:下划线; A:访问{color:#6699cc; font-weight:bold;文字装饰:下划线; A:主动{颜色:#336699; font-weight:bold;文字装饰:下划线; } .heading1 {background-color:#003366; border-bottom:#336699 6px solid;颜色:#ffffff; font-family:Tahoma; font-size:26px; font-weight:normal; margin:0em 0em 10px -20px; padding-bottom:8px; padding-left:30px; padding-top:16px;} pre {font-size:small; background-color:#e5e5cc;填充:5px; font-family:Courier New; margin-top:0px; border:1px#f0f0e0 solid;白色空间:预包装; white-space:-pre-wrap;自动换行:break-word; } table {border-collapse:collapse; border-spacing:0px; font-family:Verdana;} table th {border-right:2px white solid; border-bottom:2px白色固体; font-weight:bold; background-color:#cecf9c;} table td {border-right:2px white solid; border-bottom:2px白色固体; background-color:#e5e5cc;}< / style>

< / head>

< body>

I am trying to invoke a REST Service developed in .net with request in Json format. However, the REST service works fine with the request in xml format. Can you please let me know if there is any issue in the way the RequestFormat is defined below.

Here is the interface code:
[ServiceContract]
public interface ISalesOrderPostService
{
//POST operation
[OperationContract]
[WebInvoke(UriTemplate = "", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST")]
String PostSalesOrder(SalesOrder sales_order);
}

Here is the Response:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>



请求错误


Request Error



xmlns => ;服务器遇到处理请求的错误。有关构建对服务的有效请求,请参阅

服务帮助页。 br />


xmlns="">The server encountered an error processing the request. Please see the
service help page for constructing valid requests to the service.



< / body>

< / html>


</body>
</html>

推荐答案

试试这个链接

http://fromjami.wordpress.com/2013/05/26/call-restful-service-using-httpwebrequest-and-post-data/ [ ^ ]



希望这有帮助
Try this link
http://fromjami.wordpress.com/2013/05/26/call-restful-service-using-httpwebrequest-and-post-data/[^]

Hope this helps


这篇关于如何使用JSON请求调用Restful服务c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆