WebClient.UploadValues没有在c#中发布帖子请求

时间:2015-02-23 11:25:10

标签: c# php

我正在尝试将单个jpeg图像上传到PHP脚本。这是我的控制台应用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();
    }
}

这是我的UploadHandler课程:

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)
        );
    }
}

这是我接收上传的php脚本:

<?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");
}

这是我得到的回应:

enter image description here

知道为什么会这样吗?看来C#app没有发出HTTP POST请求。

2 个答案:

答案 0 :(得分:0)

尝试:

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

编辑: 您应该使用Fiddler调试此类问题:http://www.telerik.com/fiddler

首先确保您的C#应用​​程序通过在Fiddler中检查它来发送预期的请求,然后您可以确保您的PHP应用程序在另一端做正确的事情。

现在你不会走得太远,因为不清楚问题出在哪里。

答案 1 :(得分:0)

C#侧面

import numpy as np
import matplotlib.pyplot as plt

g = 9.81
Qave = 0.05


def efficiency(h):
    Qn = 3e-5 * np.sqrt(2 * g * h)
    NN = np.ceil(Qave / Qn)
    teta = 0.9 * np.sqrt(2 * g * h)

    if teta >= 3.025:
        Kl = 2e-5
    else:
        Kl = 2 * np.sqrt(1e-9 / np.pi * teta)
    inverse_efficiency = np.exp(-Kl * 1.2e4 * teta)
    return (inverse_efficiency)

efficiency_vector = np.vectorize(efficiency)

# plot data
h = np.arange(0.01, 100, 0.01)
efficiency = efficiency_vector(h)
plt.plot(h, efficiency)

PHP端

    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();

    }