Web服务拒绝接受JSON参数和回复

问题描述:

我试图调用从C#应用程序的Web服务(的.asmx),JSON格式。

I'm trying to call a web service (.asmx) from a c# application, in json format.

当我指定请求方法为GET,并且不指定的contentType。

When I specify request method as GET, and don't specify contentType.

(REQ是HttpWebRequest的)

(req is HttpWebRequest)

req.Method = "GET";

一切都工作得很好,但我得到XML响应。结果
当我指定的内容类型:

Everything works well, but I get XML response.
When I specify content Type:

req.ContentType = "application/json; charset=utf-8";  

我得到

500内部服务器错误。

500 internal server error.

当我改变请求方法:

req.Method = "POST";  

我只能调用参数的方法,它返回正确的json,但如果我尝试调用一个方法的参数,我再得到500错误。

I can call parameterless methods only, which returns correctly json, but if I try calling a method with parameters, I get 500 error again.

Web服务code:

The web service code:

    [WebMethod(EnableSession =true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SimplestWebService()
    {         
        return "hello";
    }  

和使用参数:

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Echo(string aString)
    {       
        return aString;
    }

任何想法将大大AP preciated。

Any ideas will be much appreciated.

补充:也许我不写POST请求权(现在是我要送它的头,就像一个GET请求)。是否有人可以指导我一下吗?

Added: Maybe I'm not writing the POST request right (now I'm sending it in the header, just like a GET request). Can someone please guide me on that?

模式又说:该网站确实被标记为脚本:

Mode added: The web site is indeed marked as script:

[ScriptService]
public class MyAPI : System.Web.Services.WebService  

这是我建立我的POST请求(我真的倾向于认为这就是问题所在):

And here is how I build my POST request (I really tend to believe that's the problem):

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(methodUrl.ToString());
req.Method = "POST";
req.Headers.Add("aString","oren");
req.ContentLength = 0;    
...
req.ContentType = "application/json; charset=utf-8";
req.Accept = "application/json; charset=utf-8";
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
  StreamReader sr = new StreamReader(res.GetResponseStream());
  result.Append(sr.ReadToEnd());
}  
...

也试过:

req.Method = "POST";          
string postData = "aString=kjkjk";
req.ContentType = @"application/json; charset=utf-8";
req.Accept = @"application/json; charset=utf-8";   
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postData);
req.ContentLength = byte1.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
newStream.Close();           

最后两个注意事项:结果
1.此Web服务在XML中使用浏览器的工作原理。结果
2.问计JSON,在code 从未达到的Web服务一个破发点。所以这可能是一个IIS(我使用的是IIS 6.1)的问题。我试过MIME类型推荐here.

Two last notes:
1. This web service works in XML using the browser.
2. Asking for json, the code never reaches a break point at the web service. So this is possibly a IIS (I'm using IIS 6.1) issue. I've tried the MIME type recommendation here.

非常感谢。

更新2:

我使用fiddler2看一个成功的POST请求(从jQuery来我ASMX服务),我看可能是值得检查的区别:

I'm using fiddler2 to look at a successful POST request (from jquery to my asmx service), and I see a difference that might be worth checking:

Accept: application/json, text/javascript, */*

这里也有一些其他的差别,但一个是突然跳出来为可能冒烟的枪唯一的一个。

There's some other differences as well, but that one is the only one that leaps out as a possible smoking gun.

更新应答(响应更新问题):

它看起来像你正确使用contentStream。但是,你推到它的数据(ASTRING = kjkjk)是无效的JSON。如下所述,您的数据可能需要在有效的JSON格式为:

It looks like you're using the contentStream correctly. However, the data you're pushing onto it ("aString=kjkjk") isn't valid JSON. As noted below, your data will likely need to be in valid JSON format:

{'aString':'kjkjk'}

原来的答复:

我相信你错误地设置ASTRING的内容。对POST方法请求,有效载荷数据不施加到头部,它是请求本身的内容。因此,你需要将其流到请求对象;看到为例此MSDN引用 。不要忘了也设置请求的内容长度。

I believe you're setting the "aString" content incorrectly. For POST-method requests, the "payload" data isn't applied to the header, it is the content of the request itself. Therefore, you need to stream it onto the request object; see this MSDN reference for an example. Don't forget to also set the request's content-length.

既然你实现通过JSON服务(并已指定JSON作为内容型),我相信你会需要一个JSON字符串转换为原始字节,而这正是你会推到流。您的JSON字符串应该是这个样子:

Since you're implementing the service via JSON (and have specified JSON as the content-type), I believe you'll need to convert a JSON string to raw bytes, and that's what you'll push it onto the stream. Your JSON string should look something like this:

{'aString':'This is the text I want to echo.'}

您可能需要做一些微调(我不是在一个地方,我可以简单的将一个确切的code样品),但这应该让你在正确的方向前进。

You might need to do some fine-tuning (I'm not at a place where I can easily put together an exact code sample), but this should get you going in the right direction.