0

Hey guys I am trying to send an XML to a URL and I am getting back a 500 error. I am not sure why since this is an external service I am trying to use.

I used the chrome extension DHC - REST/HTTP API Client to test out the url and the XML and it gets a 200 response! When I compare the feeds mine from my .NET project and the google chrome extension one these are the differences.

Google Chrome Extension Feed: 200 response

POST https://togatest.efiletexas.gov/EPayments/Webs/EPayment.aspx HTTP/1.1
Host: togatest.efiletexas.gov
Connection: keep-alive
Content-Length: 328
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36
Content-Type: text/plain
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: es,en-US;q=0.8,en;q=0.6
Cookie: _ga=GA1.2.1578362496.1438095893; _mkto_trk=id:659-PBW-104&token:_mch-efiletexas.gov-1436559093618-37466; ASP.NET_SessionId=ug3njkuiz23oq255ouwsax3w

<PaymentRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ClientKey>CJOFSTEXFILE123</ClientKey>
  <TransactionID>TESTESTTEST123</TransactionID>
  <RedirectURL>https://localhost:44300/efile</RedirectURL>
  <Amount>-1</Amount>
  <GetToken>1</GetToken>
</PaymentRequest>

My Project Feed: 500 Response

POST https://togatest.efiletexas.gov/EPayments/Webs/EPayment.aspx HTTP/1.1
Content-Type: text/plain
Accept: */*
Host: togatest.efiletexas.gov
Content-Length: 334
Connection: Keep-Alive


<PaymentRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ClientKey>CJOFSTEXFILE123</ClientKey>
  <TransactionID>TESTESTTEST123</TransactionID>
  <RedirectURL>https://localhost:44300/efile</RedirectURL>
  <Amount>-1</Amount>
  <GetToken>1</GetToken>
</PaymentRequest>

For some reason I cant get the page to load I am getting an 500 error. I believe this is due because the content length is different and I dont know why. The XML's are exactly the same but perhaps the chrome extension is doing something different when opening the connection. Any help and explanation would be awesome. If someone can tell me how to manually change the content-length that would be really cool as well.

UPDATE:

Here is the code I am using to serialize my C# object, and open the connection for the URL

PaymentRequest tokenPaymentRequest = new PaymentRequest();

            //Here we will set the values of our object
            tokenPaymentRequest.ClientKey = "CJOFSTEXFILE123";
            tokenPaymentRequest.TransactionID = "TESTESTTEST123";
            tokenPaymentRequest.RedirectURL = "https://localhost:44300/efile";
            tokenPaymentRequest.Amount = "-1";
            tokenPaymentRequest.GetToken = "1";

            var doc = new XDocument();
            var xmlSerializer = new XmlSerializer(typeof(PaymentRequest));
            using (var writer = doc.CreateWriter())
            {
                xmlSerializer.Serialize(writer, tokenPaymentRequest);
            }

            string xml = doc.ToString();

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(destinationUrl);


            //string s = "id="+Server.UrlEncode(xml);
            byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml);
            req.Method = "POST";
            req.ContentType = "text/plain";
            req.Accept = "*/*";

            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);

            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
            string backstr = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return string.Empty;
2
  • The difference in Content-Length is only 6. Excluding the last line, your request body has 6 lines. Perhaps Chrome's request contains normal line endings (\n, LF) and your second one has Windows line endings (\r\n, CR LF) and your application is not prepared to handle them? What are you using to generate these requests?
    – Rob W
    Jul 29, 2015 at 18:43
  • @RobW Hey man thanks for the reply I just added an update so you can see how I create my XML and how I am sending it. Whenever I get the req.GetRequestStream() my code breaks and it gives me the 500 error. After testing chrome a bit more i realized that the content-length does not influence the result.
    – Lostaunaum
    Jul 29, 2015 at 18:52

1 Answer 1

0

Hey guys after much tampering with the code I figured out why the external service was not accepting my request.

Basically it needed this added to the header:

req.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36";

With this in the header the code managed to be accepted successfully and the much desired 200 response has been received. I was confused at first since I thought the UserAgent property was restricted and could not be changed manually after much research I realized it can be changed if you declare your HttpWebRequest properly.

Here is a good answer for people having similar issues with sending XML's to a URL

Cannot set some HTTP headers when using System.Net.WebRequest

This answer was also very helpful when trying to understand why and how you are supposed to declare an HttpWebRequest object

How to modify request headers in c#,ASP .NET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.