创建HttpWebRequest Post请求

时间:2017-06-08 10:01:24

标签: c# .net wcf

我有一个WCF POST方法 - 它如下所示:

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "UploadReport/{customerId}/{author}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string UploadReport(string customerId, byte[] fileData, string author);

用于将报告上传到我正在使用的ECM客户端。

我通过将服务引用添加到WSDL,在另一个解决方案中使用WCF客户端调用它。如下所示:

    using (MemoryStream mem = new MemoryStream())
    {
      ExcelReportGenerator excel = new ExcelReportGenerator();

      success = excel.CreateExcelDoc(mem, customerId);
      mem.Position = 0;

      try
      {
        using (CustomerClient.UploadClient customerClient = new CustomerClient.UploadClient())
        {
          customerClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 0); 
          uploadReportId = customerClient .UploadReport(customerId.ToString(), mem.ToArray(), userId.ToString());
        }

        //remaining code removed from brevity

但是现在我需要从HttpWebRequest调用此调用,而不是使用生成的wcf客户端。

但是,我想知道如何执行此操作以便POST字节数组以及客户ID和作者。

我在

之前使用了类似下面的简单API GetRequest
  string jsonResponse = string.Empty;

  string requestUri = string.Format("{0}", myAPI);

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
  request.Credentials = new NetworkCredential(userNameWs, passwordWs);

  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    jsonResponse = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
  }

在上面的myAPI是从config加载的,如下所示:

http://localhost/myWS/api/Reference/GetAllCarManufacturers

我知道要点击我需要的WCF方法

request.Method = "POST";

我需要什么才能发布fileData并点击WCF格式为“UploadReport / {customerId} / {author}

”的帖子网址

0 个答案:

没有答案