0

This is wrecking my brain for a while now. The code works fine when I post a smaller XML string. As soon as I add more XML nodes and post, I get the infamous '404 Error'

I'm posting XML data as a string to a Generic Handler in C#.

string strXML = "Large XML Content Here";

WebClient client = new WebClient();

string xmlResult = "";

try
{
    xmlResult = client.DownloadString(_workContext.AccountingWebServiceLink 
                                    + "?action=updateprimary&xml=" 
                                    + System.Web.HttpUtility.UrlEncode(strXML));
}
catch(Exception e)
{
    System.IO.File.AppendAllText(Server.MapPath("~/addressXMLReturnError.txt"),
                         "Error: " + e.Message + " returnValue = " + xmlResult);
}

I think it might have something to do with the server not accepting large strings?

EpicKip
  • 4,015
  • 1
  • 20
  • 37
Orion
  • 452
  • 6
  • 23
  • It might be the limit so try the `` tin your web.config app, but I think the error is different in such case. – Paweł Łukasik May 18 '17 at 07:06
  • @PawełŁukasik I will give that a try now and see if it makes a difference. FYI: The string length I'm posting is 1562 long – Orion May 18 '17 at 07:11
  • If you have access to the code of the server you are sending the XML to, I would advise you to do an HTML POST, and put the XML in the body after encoding it as BASE64. https://code.tutsplus.com/tutorials/a-beginners-guide-to-http-and-rest--net-16340 – Bertus van Zyl May 18 '17 at 07:12
  • @PawełŁukasik I set the 'maxUrlLength="6144"' and still got the same error. – Orion May 18 '17 at 07:20
  • then it's not the limit but some special characters in the data. convert to BASE64 as @BertusvanZyl suggested – Paweł Łukasik May 18 '17 at 07:21
  • @PawełŁukasik will try converting to Base64. I can confirm there are no special chars in the string. – Orion May 18 '17 at 07:22
  • @Orion XML has a lot of special characters like <, & that has special meaning for URLs – Paweł Łukasik May 18 '17 at 07:24
  • @PawełŁukasik I'm using System.Web.HttpUtility.UrlEncode so it formats the special chars. Then I decode it on the server. it works fine when the xml data is smaller. So this has to be a string size issue. – Orion May 18 '17 at 07:46
  • 1
    Please see my comment above about using a POST, and putting data in body. Putting all the data in the address is like creating a textfile, and then putting all the text in the name of the file, in stead of inside the file. – Bertus van Zyl May 18 '17 at 08:19
  • @BertusvanZyl I will give it a try now and let you know how it goes. – Orion May 18 '17 at 08:30

1 Answers1

0

Finally got it working using suggestions from comments above.

So posting the XML data is the best route for me.

Using code from this SO post > HTTP post XML data in C#

To receive the data on the other side, check out this link > https://code.msdn.microsoft.com/Enviar-datos-por-el-metodo-2e580ace

Community
  • 1
  • 1
Orion
  • 452
  • 6
  • 23