简体   繁体   English

REST 服务收到 400 个错误请求

[英]REST Service Getting 400 bad request

I have created a restful web service in c#.我在 C# 中创建了一个宁静的 Web 服务。 I am getting 400 bad request error when I call the POST method.当我调用 POST 方法时,我收到 400 个错误的请求错误。 I am checking my requests in fiddler and the POST requests are perfectly fine.我正在 fiddler 中检查我的请求,POST 请求完全正常。 I don't understand what is wrong with my program.我不明白我的程序有什么问题。

Here are some of the code snippets.下面是一些代码片段。 If any of you need any other code to have a look at please ask.如果您需要任何其他代码来查看请询问。

Interface界面

[ServiceContract]
public interface IRead
{
    [OperationContract]
    [WebInvoke(UriTemplate = "GetCard", Method = "POST")]
    someObject GetCard(Session session);
}

The NFCSession is an object which has a int variable of name session. NFCSession 是一个对象,它有一个名为 session 的 int 变量。

Client generating POST request客户端生成 POST 请求

public void GetCard()
{
        string strGetCard = "http://localhost:8384/Reader/GetCard";

        byte[] dataByte = GenerateNFCSession(63315152);

        HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(strGetCard);
        POSTRequest.Method = "POST";
        POSTRequest.ContentType = "application/xml;charset=UTF-8";
        POSTRequest.ContentLength = dataByte.Length;

        Stream POSTstream = POSTRequest.GetRequestStream();

        POSTstream.Write(dataByte, 0, dataByte.Length);

        HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();
        StreamReader reader =
          new StreamReader(POSTResponse.GetResponseStream(), Encoding.UTF8);
        Console.WriteLine("Response");
        Console.WriteLine(reader.ReadToEnd().ToString());
}

XML Generator XML 生成器

private static byte[] GenerateXML(int sessionID)
    {
        MemoryStream mStream = new MemoryStream();
        XmlTextWriter xmlWriter = new XmlTextWriter(mStream, Encoding.UTF8);
        xmlWriter.Formatting = Formatting.Indented;
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("Session");
            xmlWriter.WriteStartElement("session");
                xmlWriter.WriteString(sessionID.ToString());
            xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Flush();
        xmlWriter.Close();
        Console.WriteLine(mStream.ToArray().ToString());
        return mStream.ToArray();

    }

web.config网页配置

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
  <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, 
System.Web,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>

<system.serviceModel>

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="" helpEnabled="true" faultExceptionEnabled="true"
automaticFormatSelectionEnabled="true"></standardEndpoint>
  </webHttpEndpoint>
</standardEndpoints>

</system.serviceModel>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

POST Request XML from Fiddler来自 Fiddler 的 POST 请求 XML

<?xml version="1.0" encoding="utf-8"?>
<Session>
  <session>63315152</session>
</Session>

I get this error我收到这个错误

The server encountered an error processing the request.

NOTE: When I provide XmlSerializer Format Attribute it works fine.注意:当我提供 XmlSerializer 格式属性时,它工作正常。 But I want my service to work for JSON requests as well at a later point.但我希望我的服务在以后也能用于 JSON 请求。 And from my point of view Its not necessary to explicitly provide the format attribute.从我的角度来看,没有必要明确提供格式属性。 I seriously don't understand what is wrong with this code.我真的不明白这段代码有什么问题。

Guys Please HELP!!!伙计们请帮助!!!

Try adding <enableWebScript /> inside behavior tag:尝试在行为标签中添加<enableWebScript />

...
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
      <enableWebScript />
    </behavior>
  </serviceBehaviors>
</behaviors>
...

It's discussed here: http://social.msdn.microsoft.com/Forums/en/wcf/thread/fb48999f-cd41-49b8-ae9a-f0c8f5aebbd2在这里讨论: http : //social.msdn.microsoft.com/Forums/en/wcf/thread/fb48999f-cd41-49b8-ae9a-f0c8f5aebbd2

My Web Service is working fine now.我的网络服务现在工作正常。

The changes I made are in the DataContract Attribute.我所做的更改在 DataContract 属性中。

Initially it was like this:最初是这样的:

[DataContract]

I changed it to this:我把它改成这样:

[DataContract(Namespace="")]

And it started working...它开始工作......

TIP: When you run your web service you can see sample POST requests in '/help' page of the webservice.提示:当您运行 Web 服务时,您可以在 Web 服务的“/help”页面中看到示例 POST 请求。 I never paid any attention to this page.我从来没有关注过这个页面。 But it is very useful.但它非常有用。 When I compared this example with the actual request in Fiddler I found out that the namespaces are different.当我将此示例与 Fiddler 中的实际请求进行比较时,我发现命名空间是不同的。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM