Fiddler 中的 WCF Rest 服务 POST 方法失败

发布于 2024-12-02 06:58:14 字数 2044 浏览 1 评论 0原文

我正在使用 WCF Restservice 来验证用户身份。我有一个 POST 方法,使用 jquery 工作正常,但在 Fiddler 测试中失败。我收到 415 不支持的媒体类型。

这是我的代码

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Login", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public string Login(string userName, string password)
    {
        string valid;

        // Log in user
        int authenticatedId = AuthenticateManager.Authenticate(userName, password);
        if (authenticatedId != -1)
        {
            valid = "Welcome " + userName + "!";
        }
        else
        {
            valid = "Login failed!";
        }

        return valid;
    }

这是我在 Fiddler 主体中传递的内容

[{ "userName":"dusshyi", 
   "password":"subram1" }]

这是 fiddler 生成的内容

 <HTML><HEAD><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE>
<TITLE>Service</TITLE></HEAD><BODY>
<DIV id="content">
<P class="heading1">Service</P>
<BR/>
<P class="intro">Endpoint not found.</P>
</DIV>
</BODY></HTML>

请帮忙!

I'm using WCF Restservice to authenticate user. I have a POST method and works fine using jquery but fails on Fiddler test. I get 415 Unsupported Media Type.

Here is my code

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Login", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public string Login(string userName, string password)
    {
        string valid;

        // Log in user
        int authenticatedId = AuthenticateManager.Authenticate(userName, password);
        if (authenticatedId != -1)
        {
            valid = "Welcome " + userName + "!";
        }
        else
        {
            valid = "Login failed!";
        }

        return valid;
    }

This is what I'm passing in Fiddler body

[{ "userName":"dusshyi", 
   "password":"subram1" }]

This is what fiddler generating

 <HTML><HEAD><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE>
<TITLE>Service</TITLE></HEAD><BODY>
<DIV id="content">
<P class="heading1">Service</P>
<BR/>
<P class="intro">Endpoint not found.</P>
</DIV>
</BODY></HTML>

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2024-12-09 06:58:14

您是否已在 Fiddler 的请求正文编辑器中将请求的 Content-Type 设置为 application/json

在此处输入图像描述

Have you set the Content-Type of the request to application/json in Fiddler's Request body editor?

enter image description here

月寒剑心 2024-12-09 06:58:14

今天早上我遇到了同样的问题...

为我解决的方法是单击“Composer”选项卡,然后将以下内容添加到“请求标头”部分:

Content-Type: application/x-www-form-urlencoded

没有这一行,我注意到日志显示它正在尝试使用内容类型“text/html”调用我的 REST 服务,并且该服务不会被调用。

如果我添加以下行,甚至会发生这种情况:

Content-Type: application/json

但是,在添加行“Content-Type: application/x-www-form-urlencoded”后,Fiddler 现在会调用内容类型为“application/json”的服务,并且 Web 服务被正确调用。奇怪...

我在以下页面上创建了创建 JSON WCF Web 服务并使用 Fiddler 测试它的完整演练:

http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

I had the same problem this morning...

What fixed it for me was to click on the "Composer" tab, then add the following to the "Request Headers" section:

Content-Type: application/x-www-form-urlencoded

Without this line, I noted that the log showed it was attempting to call my REST Service with a Content-Type of "text/html", and the Service wouldn't get called.

This would even happen if I added the line:

Content-Type: application/json

However, after adding the line "Content-Type: application/x-www-form-urlencoded", Fiddler would now call the service with a Content Type of "application/json", and the web service was called correctly. Strange...

I've created a complete walkthough of creating a JSON WCF Web Service, and testing it with Fiddler, on the following page:

http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

混吃等死 2024-12-09 06:58:14

Fiddler Web 调试器:Composer 部分;设置

Parsed
POST,../Service.svc/Login,HTTP/1.1

Request Header:
User-Agent: Fiddler
Content-Type: application/json;charset=UTF-8
Host: localhost:15021
Content-Length: 43

Request Body:
{"username":"ABC","password":"pwd"}

然后执行

Fiddler Web Debugger:Composer section; settings

Parsed
POST,../Service.svc/Login,HTTP/1.1

Request Header:
User-Agent: Fiddler
Content-Type: application/json;charset=UTF-8
Host: localhost:15021
Content-Length: 43

Request Body:
{"username":"ABC","password":"pwd"}

and then execute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文