You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core调用Navision Web Service返回400 Bad Request求助

Troubleshooting Navision Web Service 400 Bad Request in .NET Core (Works in Postman)

Let's break down why you're hitting that 400 Bad Request error even though Postman works, and walk through actionable fixes you can try:

First, Spot the Red Flags in Your Code

Looking at your RestSharp implementation, there are a few key issues that are likely causing the problem:

1. Hardcoded Content-Length Header

You’re manually setting the Content-Length to "3445", but this value won’t match the actual length of your XML payload when sent from .NET—even tiny whitespace differences can throw it off. Postman calculates this header automatically, so hardcoding it guarantees mismatches.

Fix: Remove this line entirely:

request.AddHeader("Content-Length", "3445");

RestSharp (and HttpClient) will handle computing the correct Content-Length for you.

2. Mismanaged NTLM Authentication

Copying the raw Authorization header from Postman is risky—NTLM tokens are session-specific and won’t work outside Postman’s context. You need to let .NET handle auth natively.

Fix for RestSharp:

// Replace your hardcoded Authorization header with this
client.Authenticator = new NtlmAuthenticator("your-username", "your-password", "your-domain");

Fix for HttpClient:

var handler = new HttpClientHandler
{
    UseDefaultCredentials = true, // Or explicitly set Credentials if needed
    AllowAutoRedirect = true
};
var client = new HttpClient(handler);

3. Invalid XML Payload

Your original XML has placeholder values (?) and random stray text like "e", "gero" scattered throughout. Even if your Fiddler capture uses a simplified version, make sure:

  • All placeholders are replaced with valid data the Navision service expects
  • The XML is fully well-formed (no unclosed tags, random text)
  • Namespace URIs match exactly what’s defined in the service’s WSDL

4. Unnecessary Manual Headers

Headers like Host, Connection, and Accept-Encoding are often handled automatically by HTTP clients. Manually setting them can cause conflicts (e.g., duplicate headers sent to the server).

Fix: Remove these lines and let the client handle them:

request.AddHeader("Host", "....");
request.AddHeader("Connection", "Keep-Alive");
request.AddHeader("Accept-Encoding", "gzip,deflate");

Step-by-Step Testing to Narrow It Down

  1. Start with the Simplified Fiddler Payload
    Use the minimal, valid XML you captured in Fiddler (the one with <ord:p_OriginType>a</ord:p_OriginType>) in your .NET code first. If this works, the issue was with your original complex XML.

  2. Enable Debug Logging
    For RestSharp, log the exact request being sent to compare side-by-side with Postman’s working request in Fiddler:

    client.Log = new DebugLogProvider(); // Implement ILogProvider if needed
    

    This will show you raw headers and body, so you can spot differences like encoding or whitespace.

  3. Validate XML Against the WSDL
    Use a tool like XML Schema Validator to check that your payload matches the schema defined in the Navision service’s WSDL. Navision is strict about schema compliance.

Example Fixed RestSharp Code

Here’s a cleaned-up version of your code with all fixes applied:

var client = new RestClient("http://..../Codeunit/OrderIntegrationXML");
client.Timeout = -1;
// Handle NTLM auth properly
client.Authenticator = new NtlmAuthenticator("your-username", "your-password", "your-domain");

var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/xml;charset=UTF-8");
request.AddHeader("SOAPAction", "urn:microsoft-dynamics-schemas/codeunit/OrderIntegrationXML:InsertOrderXMLWS");

// Use the valid, simplified XML from your Fiddler capture
var xmlPayload = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ord=""urn:microsoft-dynamics-schemas/codeunit/OrderIntegrationXML"" xmlns:x62=""urn:microsoft-dynamics-nav/xmlports/x62000"">
 <soapenv:Header/>
 <soapenv:Body>
 <ord:InsertOrderXMLWS>
 <ord:p_OriginType>a</ord:p_OriginType>
 <ord:p_OriginOrderNo>z</ord:p_OriginOrderNo>
 </ord:InsertOrderXMLWS>
 </soapenv:Body>
</soapenv:Envelope>";

request.AddParameter("text/xml;charset=UTF-8", xmlPayload, ParameterType.RequestBody);

IRestResponse responseZ = client.Execute(request);
Console.WriteLine($"Status Code: {responseZ.StatusCode}");
Console.WriteLine(responseZ.Content);

Extra Navision-Specific Tips

  • Double-check date formats (Navision typically expects yyyy-MM-dd for date fields)
  • Reuse HttpClient instances instead of creating new ones each time (prevents auth and connection issues)
  • If you’re using a complex XML payload, consider serializing a strongly-typed class to XML instead of using raw strings—this reduces formatting errors.

内容的提问来源于stack exchange,提问作者abraxas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:02:50