0

So the issue is the POST request is not not correct I am guessing and therefore the php is not able to read it properly.

I will post the php and c# code of request below. But I believe the main issue is the c# code of the request.. Thanks for the help.

C# Code:

string screenshot = @"C://1478941.jpg";

var request = (HttpWebRequest)WebRequest.Create("https://ADDRESS/screenshots/index.php?");  
string postData = "&access_token=" + label11.Text;
postData += "&screenshot=" + screenshot;
byte[] data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (Stream stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

string xml = new StreamReader(response.GetResponseStream()).ReadToEnd();

PHP Code:

$post_access_token = $_POST['access_token'];



$uid = $api->verifyUser($post_access_token);

if (isset($_FILES['screenshot']) && $_FILES['screenshot']['error'] != 4) {

    $screenshot_file = $_FILES['screenshot'];

    if(is_uploaded_file($screenshot_file['tmp_name'])){

        $tokenGen = new TokenGen();
        $token = $tokenGen->genToken('',40);

        $screenshot_file_name = $page->sanitize($screenshot_file['name']);
        $screenshot_file_name = basename($screenshot_file_name);
        $file_extension = explode('.', $screenshot_file_name);
        $file_extension = strtolower(end($file_extension));
        $screenshot_file_name = $uid.'_'.$token.'.'.$file_extension;
        $file_size = $screenshot_file['size'];
        $file_tmp = $screenshot_file['tmp_name'];
        $file_error = $screenshot_file['error'];

        $allowed_file_extensions = 'jpg, jpeg, png, gif';

        if (strpos($allowed_file_extensions,$file_extension) === false) {
            $errors[] = 1;
        }

        if ($file_size > 3000000) {
            $errors[] = 1;
        }

        if (!$file_error == 0) {
            $errors[] = 1;
        }
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • Run Fiddler, and see the HTTP POST that is captured when you execute the PHP code - then you can model your c# code from that – Cam Bruce Sep 12 '14 at 16:47
  • 4.0 is the version of .net framework required – Evan coding Sep 12 '14 at 16:57
  • 2
    wrong mime type. php expects "multipart/form-data" for uploads. – Marc B Sep 12 '14 at 16:58
  • still doesn't work with multi form... – Evan coding Sep 12 '14 at 17:08
  • It looks like [How does HTTP file upload work?](http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work) may be a good read to get the low level idea of what to do. Furthermore. the `screenshots` variable is a `String` equal to the file path, not the actual data for the image. You will need to open that file and send its contents across the network in a multi part HTTP post body. – Greg Burghardt Sep 12 '14 at 17:10
  • Can you give me an example of how I would change my code to do that? – Evan coding Sep 12 '14 at 17:12

0 Answers0