0

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.

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. By 'long' I mean that when I deserialize the string and export it as an XML file it takes up about 200 kB. However, I'm not getting anywhere with a short JSON string, either.

I tried using RestSharp but I keep getting an "Endpoint not found." 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." or, if I send a short JSON string, I get an "Error 400: Bad Request."

Here are the methods I used.

Restsharp: RestSharp simple complete example

(here I used the first answer with 141 votes) .NET: Simplest way to send POST with data and read response

https://msdn.microsoft.com/en-us/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. However, an answer in PHP or AJAX won't do me much good. 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.

I would appreciate any help. Thanks in advance.

P.S. This is his AJAX code in the HTML file he sent me - and it works. I took out the URL for safety purposes, and I replaced the long JSON string with a simple one.

<!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>
Community
  • 1
  • 1
  • 2
    Welcome to StackOverflow. A few notes about asking questions here: 1) Try to ask one question per question. Questions with multiple, independent parts get fewer responses. 2) Show what you have done so far - VB.NET or c#. Thus I'd suggest retiring this question and asking 2 questions similar to "How to post large JSON string with RestSharp" and "How to post large JSON string with WebClient". Including the working PHP code in both is fine. – dbc May 09 '15 at 15:32
  • If you use chrome I recommend taking a look at postman. It's free and allows you to post to your webservice without using your client. This way you can find out if its a problem with the client or the server. and if you're not using chrome I recommend you start (if possible)! – mattumotu May 27 '15 at 11:20
  • I understand why you want to remove the url, but consider: this also removes the endpoint, which makes it hard for us to help you. If this is meant to be a secure service they you will need to actually secure it, simply hiding it isn't enough. And if its public facing then why hide it here? – mattumotu May 27 '15 at 11:22

1 Answers1

0

It sounds like the 413 error kicks in earlier and hides to endpoint not found/ 400 error. IIRC the default max request entity is 65k, so thats why the you get the 413 error. 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. You will probably need to create a new binding configuration with a large MaxReceivedMessageSize. (again don't make it too big or risk DOS attacks). You may find you need to change the MaxStringContentLength also. Then set your endpoints to use this new binding configuration. This should deal with the 413 error.

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.

mattumotu
  • 1,436
  • 2
  • 14
  • 35