简体   繁体   English

POST请求-较长的JSON字符串-请求大小太大/找不到端点

[英]POST request - Long JSON string - request size too large / Endpoint not found

This is my first posting here, so please forgive if I break any rules. 这是我第一次在这里发布信息,因此,如果我违反任何规则,请原谅。 I have searched stackoverflow extensively but I wasn't able to find an answer for my problem. 我已经广泛搜索了stackoverflow,但是找不到我的问题的答案。

Basically, I am trying to send a long JSON string from a Windows desktop application as the body of a POST request to a WCF service. 基本上,我试图将Windows桌面应用程序中的长JSON字符串作为POST请求的主体发送到WCF服务。 By 'long' I mean that when I deserialize the string and export it as an XML file it takes up about 200 kB. “长”是指当我反序列化字符串并将其导出为XML文件时,它占用了大约200 kB。 However, I'm not getting anywhere with a short JSON string, either. 但是,我也没有带短JSON字符串的地方。

I tried using RestSharp but I keep getting an "Endpoint not found." 我尝试使用RestSharp,但始终收到“找不到端点”。 error. 错误。 When I try a method I found on StackOverflow or the default method from MSDN, I get an error saying: "Error 413: Request Entity Too Large." 当我尝试在StackOverflow上找到的方法或MSDN的默认方法时,出现错误消息:“错误413:请求实体太大。” or, if I send a short JSON string, I get an "Error 400: Bad Request." 或者,如果我发送短JSON字符串,则会收到“错误400:错误的请求”。

Here are the methods I used. 这是我使用的方法。

Restsharp: RestSharp simple complete example Restsharp: RestSharp简单完整的示例

(here I used the first answer with 141 votes) .NET: Simplest way to send POST with data and read response (在这里,我以141票获得了第一个答案) .NET:发送带有数据并读取响应的POST的最简单方法

https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.100%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-27 https://msdn.microsoft.com/zh-CN/library/debx8sh9%28v=vs.100%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-27

I use VB.NET, but I'll gladly take an answer in C# as well. 我使用VB.NET,但我也会很高兴在C#中回答。 However, an answer in PHP or AJAX won't do me much good. 但是,用PHP或AJAX给出的答案对我没有多大帮助。 I've already had a coworker implement this in AJAX and it works, but we're building the Windows desktop application in VB.NET and AJAX doesn't exactly translate word-for-word into VB.NET. 我已经有一个同事在AJAX中实现此功能,并且它可以工作,但是我们正在VB.NET中构建Windows桌面应用程序,而AJAX并未将逐字转换为VB.NET。

I would appreciate any help. 我将不胜感激任何帮助。 Thanks in advance. 提前致谢。

PS This is his AJAX code in the HTML file he sent me - and it works. PS这是他发送给我的HTML文件中的他的AJAX代码-并且有效。 I took out the URL for safety purposes, and I replaced the long JSON string with a simple one. 为了安全起见,我取出了URL,然后用一个简单的字符串替换了长长的JSON字符串。

<!DOCTYPE HTML>
<html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script type="text/javascript">

function btn_click(){
//alert('btn');
var updatedData='[{"firstname": "Billy", "lastname": "Bob", "occupation": "cowboy"}]';
$.ajax({
        type: "POST",
        url:"<suppressed>",
        data: JSON.stringify(updatedData),
        contentType: "application/json; charset=utf-8?",
        dataType: "json",
        processData: true,
        success: function (data, status, jqXHR) {
                  alert("success…" + data);
        },
        error: function (xhr) {
               $('#msgText').text(xhr.responseText);
                     //alert(xhr.responseText);
        }
});

}
</script>
</head>
<body>
<input type="button" text="Click" Value="Click" onclick="btn_click()">
<label id="msgText"></label>
</body>

It sounds like the 413 error kicks in earlier and hides to endpoint not found/ 400 error. 听起来像是出现了413错误,并且隐藏到未找到的端点/ 400错误。 IIRC the default max request entity is 65k, so thats why the you get the 413 error. IIRC的默认最大请求实体为65k,因此这就是为什么您会收到413错误的原因。 This max size is there to try and help stop any denial of service attacks. 可以使用此最大大小来尝试阻止任何拒绝服务攻击。 If you want to change this the easiest way would be to use the WCF Service Configuration Editor (VS2010: Tools > WCF Service Configuration Editor) Open the WCF services config file. 如果要更改此设置,最简单的方法是使用WCF服务配置编辑器(VS2010:工具> WCF服务配置编辑器)。打开WCF服务配置文件。 You will probably need to create a new binding configuration with a large MaxReceivedMessageSize. 您可能需要创建具有大MaxReceivedMessageSize的新绑定配置。 (again don't make it too big or risk DOS attacks). (再次不要使其太大或冒DOS攻击的风险)。 You may find you need to change the MaxStringContentLength also. 您可能会发现还需要更改MaxStringContentLength。 Then set your endpoints to use this new binding configuration. 然后将您的端点设置为使用此新的绑定配置。 This should deal with the 413 error. 这应该处理413错误。

The endpoint not found is a little more complex. 找不到端点比较复杂。 As it implies your client is trying to call an endpoint on the server which isn't being found. 暗示您的客户端正在尝试调用服务器上未找到的端点。 As you haven't included details of the endpoint your client is trying to call or the services/endpoints the server offers there isn't a lot I can do to help. 由于您没有包括您的客户端尝试呼叫的端点的详细信息或服务器提供的服务/端点,因此我无能为力。 However again in the Service Config Editor you can go to diagnostics and turn on tracing. 但是,再次在服务配置编辑器中,您可以转到诊断并打开跟踪。 This will create a trace file on the server which may help you spot the problem. 这将在服务器上创建一个跟踪文件,这可以帮助您发现问题。

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

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