【问题标题】:Post file with C# [duplicate]使用 C# 发布文件 [重复]
【发布时间】:2017-07-17 03:47:09
【问题描述】:

我想将文件发送到具有其他参数的 Web 服务
像这样:

param1=value1&param2=value2&param3=IMAGE_FILE

我如何在 C# 中做到这一点
我试试这个:

&param3=Convert.ToBase64String(File.ReadAllBytes(txtlogo.Text))

但那行不通..
那么有什么解决办法吗?

更新:

我也尝试像这样使用HttpContent(我不知道它是否正确)

HttpContent stringContent = new StringContent("param1=param1&param2=param2");
            FileStream file = new FileStream(txtlogo.Text, FileMode.Open);
            HttpContent fileStreamContent = new StreamContent(file);
            using (var client = new HttpClient())
            using (var formData = new MultipartFormDataContent())
            {
                formData.Add(stringContent, "param1", "param1");
                formData.Add(fileStreamContent, "file1", "file1");
                var response = client.PostAsync("url", formData).Result;
                if (!response.IsSuccessStatusCode)
                {
                   string ss = "s";
                }
                string my = response.Content.ReadAsStringAsync().Result; 
            }

并且服务器没有收到带有POST 的任何数据...

【问题讨论】:

  • 当你说它不起作用时?您是否尝试在服务器上读取param3 却没有得到图像文件?
  • @Christos 是的,但那行不通,我不知道为什么
  • 特别是重复问题的答案:stackoverflow.com/a/28242511/413399
  • @DavidCulp:如果您发送答案,我会接受 :)
  • 感谢您的提议,但无法发布标记为重复的问题的答案。只需对适合您的问题的答案投票即可。

标签: c# post parameters httpwebrequest


【解决方案1】:

用get方法试试(简单)

    try
{
    using (var wc = new WebClient())
    {
        string url = "https://url.com/index.php?x=1222&y=filebase64-sdfsdfsdfsdfsdfsdfsdfsdfsd";
        // Get account from accessToken
        response = wc.DownloadString(url);
    }
}
catch (Exception ee)
{
    Console.WriteLine("Coś nie tak z pobieraniem kont");
}

【讨论】:

猜你喜欢
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
  • 2012-05-01
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多