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?