简体   繁体   中英

WebClient.UploadValues is not making a post request in c#

I am trying to upload a single jpeg image to a PHP script. This my console app program.cs :

class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        string result = UploadHandler.Post(
            "http://localhost/upload_test",
            "frame.jpg"
        );

        stopWatch.Stop();
        TimeSpan ts = stopWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);

        Console.WriteLine("Result : {0}", result);
        Console.WriteLine("File uploaded in : {0}", elapsedTime);
        Console.ReadKey();
    }
}

This is my UploadHandler class:

class UploadHandler
{
    public static string Post(string serverUrl, string filePath)
    {
        string result = "";
        using (WebClient client = new WebClient())
        {
            byte[] response = client.UploadValues(serverUrl, new NameValueCollection()
            {
                { "frameData", ToBase64(filePath) }
            });
            result = Encoding.Default.GetString(response);
        }
        return result;
    }

    private static string ToBase64(string filePath)
    {
        return Convert.ToBase64String(
            File.ReadAllBytes(filePath)
        );
    }
}

and this is my php script that receives the upload:

<?php

if (count($_POST) && isset($_POST['frameData']))
{
    file_put_contents('frame.jpg', base64_decode($_POST['frameData']));
    exit("OK");
}
else
{
    print_r($_POST);
    exit("INVALID REQUEST");
}

And this is the response I get:

在此处输入图片说明

Any idea why this might be? It appears the C# app isn't making a HTTP POST request.

Try:

client.UploadValues(serverUrl, "POST", new NameValueCollection()
            {
                { "frameData", ToBase64(filePath) }
            });

EDIT: You should debug issues like this with Fiddler: http://www.telerik.com/fiddler

First make sure that your C# app sends the expected request by inspecting it in Fiddler, and then you can make sure that your PHP app is doing the right thing on the other end.

Right now you won't get very far, because it is not clear where the problem is.

C# Side

    private void button1_Click(object sender, EventArgs e)
    {
        string URL = "http://localhost/phppost/upload.php";
        WebClient webClient = new WebClient();

        Byte[] bytes = File.ReadAllBytes("31.jpg");
        String file = Convert.ToBase64String(bytes);

        NameValueCollection formData = new NameValueCollection();
        formData["user"] = "Brasd1";
        formData["pass"] = "85s1a";
        formData["name"] = "31.jpg";
        formData["image"] = file;

        byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
        string responsefromserver = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responsefromserver);
        webClient.Dispose();

    }

PHP Side

<?php

$user = isset($_POST['user']);
$pass = isset($_POST['pass']);
$name = isset($_POST['name']);
$imag = isset($_POST['image']);

if ($user === 'Brasd1' && $pass === '85s1a' && $name != '' && $imag != '') {
    base64_to_jpeg($_POST['image'], $name);
} else {
    echo "Error user pass file";
}

function base64_to_jpeg($base64_string, $output_file)
{
    $ifp = fopen('uploads/' . $output_file, "wb");
    fwrite($ifp, base64_decode($base64_string));
    fclose($ifp);
    return ($output_file);
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM