dsyua2828 2015-02-23 11:25
浏览 101

WebClient.UploadValues没有在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:

enter image description here

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

  • 写回答

1条回答 默认 最新

  • douqihua6212 2015-02-23 11:36
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?