0

I want to send an XML file to a specific website, but there are some tricky things, what I cannot resolve with the HttpWebResponse class.

The documentation gives me a basic html form (what I want to convert into C# code):

<html>
  <head><meta content="text/html; CHARSET=UTF-8"></head>
    <body>
      <form action="https://www.targetsite/" method="post" enctype="multipart/form-data">
        XML file: <input type="file" name="action-xmlgenerate"><br><br>
        <input type="submit" name="generate" value="Generate">
      </form>
    </body>
</html>

The documentation also tells:

The request must be “multiform/form-data” type and must contain one file with the name action-xmlgenerate

I do not want to upload the XML file from computer, I have to generate the XML file in the code and it have to have a specific name: action-xmlgenerate

I tried this:

Xml class:

[XmlRoot(ElementName = "seller")]
public class Seller
{
    [XmlElement(ElementName = "bank")]
    public string Bank { get; set; }
    [XmlElement(ElementName = "creditCardNumber")]
    public string CreditCardNumber { get; set; }
    [XmlElement(ElementName = "emailReplyto")]
    public string EmailReplyto { get; set; }
    [XmlElement(ElementName = "emailSubject")]
    public string EmailSubject { get; set; }
    [XmlElement(ElementName = "emailText")]
    public string EmailText { get; set; }
}

Communication function:

public void sendXml()
{
    Seller seller = new Seller();
    seller.Bank = "asd";
    seller.CreditCardNumber = "1234";
    seller.EmailReplyto = "a@a.a";
    seller.EmailSubject = "asd";
    seller.EmailText = "asdasd";

    var xmlFile = seller;

    byte[] bytes;
    bytes = Encoding.ASCII.GetBytes(xmlFile.ToString());

    string boundary = $"---------------------------{DateTime.Now.Ticks.ToString("x")}";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://targetsite/");
    request.ContentType = "multipart/form-data";
    request.Method = "POST";
    request.KeepAlive = true;
    request.Credentials = CredentialCache.DefaultCredentials;

    request.Headers.Add("name=\"action-xmlgenerate\"");
    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();
    }
}
        

What can be a solution?

Business Man
  • 81
  • 1
  • 6
  • What's the issue you are facing? – Chetan Jan 04 '21 at 02:17
  • It does not send my request to the proper endpoint. I think there is some trivial mistakes that I made. – Business Man Jan 04 '21 at 02:34
  • This could be the answer you are looking for https://stackoverflow.com/a/19983672/5417823 – mylee Jan 04 '21 at 03:25
  • @BusinessMan What does this line do : `var xmlFile = Seller;` ? Did you mean `var xmlFile = seller;` ? – Peter Csala Jan 04 '21 at 07:47
  • @PeterCsala Yes, I pasted here it incorrectly. – Business Man Jan 04 '21 at 07:58
  • @BusinessMan Have you overwritten the `ToString` of `Seller` class? If not then your `GetBytes` method call will receive only the class name. – Peter Csala Jan 04 '21 at 08:00
  • @PeterCsala Yeah, I made some modification, my problem is that in the response object I got back the html code of targetsite, not the response xml what I need to get. – Business Man Jan 04 '21 at 08:21
  • @BusinessMan Please try to share with us a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Peter Csala Jan 04 '21 at 08:24
  • @PeterCsala Sorry, I cannot really do it, I just want to know how to send a proper Http request which type is multiform/form-data, and which contains an xml file which xml file is a string in the code. You can see the HTML code, I want to "transform" it to a C# http request. I thought it is simple, but I got stucked. – Business Man Jan 04 '21 at 08:57
  • Use following to encode html special characters : System.Net.WebUtility.HtmlEncode(xml string) – jdweng Jan 04 '21 at 09:08

1 Answers1

0

I think, I already have found a similar type of solution on stack overflow. Please check the green ticked marked answer, HTTP post XML data in C#

For you help I have pasted the code from those links:

public string ToXML(Seller obj)//object to XML link: https://stackoverflow.com/questions/11447529/convert-an-object-to-an-xml-string
{
    using (var stringwriter = new System.IO.StringWriter())
    {
        var serializer = new XmlSerializer(obj.GetType());
        serializer.Serialize(stringwriter, this);
        return stringwriter.ToString();
    }
}

public string sendXml(string destinationUrl, string requestXml)// link: https://stackoverflow.com/questions/17535872/http-post-xml-data-in-c-sharp
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
    byte[] bytes;
    bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
    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();
        return responseStr;
    }
    return null;
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Srijon Chakraborty
  • 2,007
  • 2
  • 7
  • 20
  • Unfortunately it does not working: the ContentType have to be "multipart/form-data" and in this code where can I specify that information: <- input file name attribute must be action-xmlgenerate. – Business Man Jan 04 '21 at 11:40