RESTful WCF json 请求和 json 响应返回 null
我正在尝试为 RESTful WCF 构建一个示例。请求和响应都是 JSON。我得到的答复是:
{"FirstName":null,"LastName":null}
我需要得到适当的答复。
代码如下:
Web.config 有 Restful 的配置:
服务契约:
[OperationContract]
[WebInvoke(UriTemplate = "Data",
ResponseFormat = WebMessageFormat.Json)]
person getData(person name);
实现:
public person getData(person name)
{
return new person{ FirstName= name.FirstName, LastName= name.LastName };
}
[DataContract]
public class person
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
}
客户端:
class Program
{
static void Main(string[] args)
{
string baseAddress = "http://localhost/RESTfulService";
SendRequest(baseAddress + "/Data", "POST", "application/json", @"{""getData"" : {""name"" :{""FirstName"":""John"", ""LastName"":""Doe""}}");
}
public static string SendRequest(string uri, string method, string contentType, string body)
{
string responseBody = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Method = method;
if (!String.IsNullOrEmpty(contentType))
{
req.ContentType = contentType;
}
if (body != null)
{
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
req.GetRequestStream().Close();
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
resp = (HttpWebResponse)e.Response;
}
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
foreach (string headerName in resp.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
}
Console.WriteLine();
Stream respStream = resp.GetResponseStream();
if (respStream != null)
{
responseBody = new StreamReader(respStream).ReadToEnd();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
}
Console.WriteLine();
Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
Console.WriteLine();
return responseBody;
}
}
I am trying to build a sample for RESTful WCF. The request and response is JSON. The response that I get is:
{"FirstName":null,"LastName":null}
I need to get proper response.
Here is the code:
Web.config has configuration for Restful:
service contract:
[OperationContract]
[WebInvoke(UriTemplate = "Data",
ResponseFormat = WebMessageFormat.Json)]
person getData(person name);
Implementation:
public person getData(person name)
{
return new person{ FirstName= name.FirstName, LastName= name.LastName };
}
[DataContract]
public class person
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
}
Client:
class Program
{
static void Main(string[] args)
{
string baseAddress = "http://localhost/RESTfulService";
SendRequest(baseAddress + "/Data", "POST", "application/json", @"{""getData"" : {""name"" :{""FirstName"":""John"", ""LastName"":""Doe""}}");
}
public static string SendRequest(string uri, string method, string contentType, string body)
{
string responseBody = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Method = method;
if (!String.IsNullOrEmpty(contentType))
{
req.ContentType = contentType;
}
if (body != null)
{
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
req.GetRequestStream().Close();
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
resp = (HttpWebResponse)e.Response;
}
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
foreach (string headerName in resp.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
}
Console.WriteLine();
Stream respStream = resp.GetResponseStream();
if (respStream != null)
{
responseBody = new StreamReader(respStream).ReadToEnd();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
}
Console.WriteLine();
Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
Console.WriteLine();
return responseBody;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
寒江雪…2024-10-15 20:32:06
问题:
的空 JSON 响应
界面:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "Client/{idsHashed}", ResponseFormat = WebMessageFormat.Json)]
Summary GetClientDataById(string idsHashed);
Web 方法:
public Summary GetClientDataById(string idsHashed)
{
Summary clientSum = new Summary().GetClientDataById(clientId);
return clientSum;
}
在类中,我已将字段转为内部字段和私人!
public class Summary
{
private string clientName { get; set; } //<-- wont be rendered out as json because its private
解决方案:
检查类属性是否为公共。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
不知何故,它只适用于“GET”方法。
对于 POST 方法,我必须在客户端以不同的方式调用服务操作:
somehow it works only fine for "GET" methods.
For POST methods, I had to call the service operations in different manner on the client side: