【问题标题】:Upload a file via POST using RestSharp使用 RestSharp 通过 POST 上传文件
【发布时间】:2014-06-16 11:07:35
【问题描述】:

我正在尝试用 C# 模拟一个简单的多部分 HTML 表单。

我使用addParameter 函数成功地将文本作为参数发布。 现在我想使用 POST 上传文件。我尝试使用RestRequest 对象的addFile 函数。但它没有用!我收到响应代码:0。

这是我的代码:

//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://" + ipTextBox.Text + "/samplepost/postdata.php", Method.POST);

request.AddParameter("msgpost", msgTextBox.Text);
request.AddFile("file1", "NEVER.jpg");

//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
     if (response.StatusCode == HttpStatusCode.OK)
     {
          //POST successful
          MessageBox.Show("Success!");                   
     }
     else
     {
          //error ocured during POST
          MessageBox.Show(":-(\nFailed.\nError: " + response.ErrorMessage);
     }
});

请帮我找出我的代码中的错误。 提前致谢!

【问题讨论】:

标签: c# post restsharp


【解决方案1】:

NEVER.jpg 是否与 exe 位于同一位置?因为您调用 AddFile 的方式假定它是。

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2012-06-18
    • 2017-12-24
    • 2013-11-29
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多