1

I have a problem in "translating" this HTML page into c # code. I have to pass an xml file to a production machine and I would like to do it from a c # application instead of manually, as shown in the attached screenshot.

I wrote this code c #:

WebRequest request = WebRequest.Create(@"http://Machine_IP/JTI/");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(d.InnerXml);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
     Stream responseStream = response.GetResponseStream();
     string responseStr = new StreamReader(responseStream).ReadToEnd();

}

I can't figure out how to pass name = "ImportJobs". If I make a "generic" POST, the machine does not receive the xml file. I should do a POST as an ImportJobs method.

The supplier, as specifications, gives me the following:

Request

HTTP method: POST
Encryption type (Enctype): Multipart/form-data
URL: http://MachineName/JTI
Command: ImportJobs
Parameters: None
Multipart data section:The XML job description

Response

Data none

Can anyone help me? thanks a lot

HTML Example

  • 1
    @germi that depends on the code that handles the upload, it can definitely require to have a value set for ImportJobs otherwise it would not run. Simple PHP example: `if (!isset($_POST["ImportJobs"])) exit();` Or even if multiple "commands" exist in a single page it would be used to determine which command should be executed. – Longoon12000 Oct 25 '19 at 11:31
  • You may need to set the parameter name for the file being sent to ImportJobs. A similar post on how to do this with HttpClient is available at https://stackoverflow.com/a/20005964/3768875 You may be able to use that as a starting point for how to do it in WebRequest or change to use HttpClient, which is what a lot of people have moved to over the last few years. – cminus Oct 25 '19 at 11:32
  • @germi Exactly, on the page there are more "Command" and I have to pass the correct "Command". – Andrea Gennaioli Oct 25 '19 at 12:07
  • Do you use a postman app to post any data? what's the code you received? @AndreaGennaioli – CelzioBR Oct 25 '19 at 14:45
  • @CelzioBR i Use c# to write code that post data. The application is written by me. – Andrea Gennaioli Oct 25 '19 at 15:22
  • @AndreaGennaioli Before making calls in C # I always use postman to validate if the other side is correct. in my case I find it easier – CelzioBR Oct 25 '19 at 15:23
  • @CelzioBR I downloaded Postman, verified the request and adding the parameters works correctly. But from my c # code it doesn't work. – Andrea Gennaioli Oct 28 '19 at 08:06
  • @CelzioBR the code generated for the parameters is as follows: request.AddParameter ("multipart / form-data; boundary = ---- WebKitFormBoundary7MA4YWxkTrZu0gW", "------ WebKitFormBoundary7MA4YWxkTrZu0gW r nContent-Disposition: form-data; name =" F1 "; filename = "JTI.xml" Content - Type: application / xml r - n ------ WebKitFormBoundary7MA4YWxkTrZu0gW r Content of the form: data - name; ImportJobs - importJobs r - Import WebKitFormBoundary7MA4YWxkTrZu0gW-- ", ParameterType.RequestBody); The JTI.xml file how do I pass it ??? – Andrea Gennaioli Oct 28 '19 at 08:07
  • @AndreaGennaioli https://stackoverflow.com/questions/47295675/how-do-i-post-xml-data-to-a-webservice-with-postman. Try this – CelzioBR Oct 28 '19 at 18:23

0 Answers0