WebClient发布数据问题

时间:2017-06-15 06:32:53

标签: c# webclient

我想使用带有webclient的REST API,这是我的代码和它的抛出

  

" validationObjective"没有在请求中发送。

但我在我的帖子中添加了这个验证目标

有人可以为此提供帮助吗?

WebClient webClient = new WebClient();
string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
webClient.Headers.Add("Content-Type", Convert.ToString("multipart/form-data; validationObjective=\"test\";boundary=") + boundary);
var fileData = webClient.Encoding.GetString(xmlFile);

var package = string.Format("--{0}" + Environment.NewLine + "Content-
    Disposition: form-data; name=\"ccdaFile\";validationObjective=\"test\";
    filename=\"{1}\"" + Environment.NewLine+ "Content-Type: {2}" 
    + Environment.NewLine + "{3}" + Environment.NewLine + "--{0}--" 
    + Environment.NewLine, boundary, "filename", "multipart/form-data", fileData);

var nfile = webClient.Encoding.GetBytes(package);
var resp = webClient.UploadData("url", "POST", nfile);
var bytesAsString = Encoding.ASCII.GetString(resp);

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我们需要像querystring一样发送参数

 webClient.QueryString.Add("referenceFileName", "sdfdsF");
 webClient.QueryString.Add("validationObjective", "sdfdsF");

这对我有用!