0

I have a WCF POST Method - it looks like below:

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

It is used to upload a Report to an ECM Client I am using.

I was calling this with a WCF Client in another solution by adding the Service reference to the WSDL. So as below:

    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

However now I need to make this call from a HttpWebRequest rather than using the generated wcf client.

However, I am wondering how to do this in order to POST the byte array as well as the customer id and author.

I have used something like the below for a simple API GetRequest before

  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();
  }

IN the above myAPI was loaded from config and looked like below:

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

I know to make it hit my WCF Method I would need

request.Method = "POST";

What would I need in order to POST the fileData and hit the Post URL which in the WCF is in the format "UploadReport/{customerId}/{author}

Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116
  • What are the contents of the myAPI variable? – mjwills Jun 08 '17 at 10:06
  • Do https://stackoverflow.com/questions/12972400/posting-byte-array-to-the-server or https://stackoverflow.com/questions/8911362/passing-binary-data-through-httpwebrequest help? – mjwills Jun 08 '17 at 10:07

0 Answers0