0

I am trying to send base64 encoded data through C# WebRequest to php script. Then I receive sent data by file_get_contents("php://input"), when data length is lower than 8 KBs the php code is executed, otherwise it can't be executed. In the other hand php didn't receive the request.

the code C#:

            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            // postData is Base64_encoded
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;

            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseData = reader.ReadToEnd();
            responseData = SecurityUtils.DecryptUrlData(responseData);
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseData;

Could any one help?

mbmsit
  • 63
  • 1
  • 7
  • 3
    There are limits on how long a GET request can be, you're likely hitting those. Can you use POST? – Pekka Jan 26 '13 at 20:41
  • 1
    Could you show your C# code that is performing the request? – Darin Dimitrov Jan 26 '13 at 20:45
  • Look at the web servers logs. If you can't find the request there, try setting up wireshark and see if you can find out if the request is actually coming through. – troelskn Jan 26 '13 at 21:00

2 Answers2

0

The below is incorrect (mea culpa). You cannot use enctype=multipart/form-data with php://input. See here

Perhaps you can post your PHP code into your question as well?


Make sure that when you POST from the c# app to your PHP app that you set the enctype to multipart/form-data.

Explanaition

Are you using application/x-www-form-urlencoded or multipart/form-data? There is a good explanation of the differenes here: application/x-www-form-urlencoded or multipart/form-data?

According to the link above, posting data in the application/x-form-urlencoded format will put the body into the URL. Many servers and languages (PHP, Apache) will cut off or throw errors when the URL is greater than 8K. See SO question: What is the maximum length for a URL?

Community
  • 1
  • 1
Sam Texas
  • 1,245
  • 14
  • 30
  • i replaced "contentType" value to "multipart/form-data" but now file_get_contents("php://input") result is empty always, why? – mbmsit Jan 26 '13 at 21:07
  • i replaced 'contentType' value to 'multipart/form-data' but now file_get_contents('php://input') result is empty always, why? did it deference from application/x-www-form-urlencoded's output? – mbmsit Jan 26 '13 at 21:35
0
  1. Change the content type for the request

    request.ContentType = "text/plain";

  2. Check out php.ini´s post_max_size on the server side

moander
  • 2,080
  • 2
  • 19
  • 13