I have created a test html form using the code shown here which, when I enter a value in the Key textbox, select the first of the two Types and click the Continue button the browser window will navigate to an XML version of a web page. If I replace XML with JSON on the form action line, it would render the same web page in a JSON format. Replace it with SUBMIT and the resulting web page would be just a normal web page.
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="https://www.someaddress.com/xxxxxxxxxx/999999999/XML" method="post">
<table role="presentation">
<tr>
<th><label for="key">Key</label></th>
<td><input type="text" id="key" name="key" size="10" autofocus> </td>
</tr>
<tr>
<th>Type</th>
<td>
<label><input type="radio" name="type" id="type1" value="type1"> Type 1</label>
<label><input type="radio" name="type" id="type2" value="type2"> Type 2</label>
</td>
</tr>
<tr>
<td></td>
<td><button type="submit" class="btn btn--primary">Continue</button></td>
</tr>
</table>
</form>
</body>
</html>
The problem I'm having is with an attempt I'm making to convert the action of the web form above into a method of a console application below. When using WebClient (which is how this application interacts with the web in several other places), how should the value of the string variable "data" be written so that it will be posted to the destination ip address being passed into the method and return a string containing the XML data that is the web page? I've tried the line shown below (which was taken from one web page that I found when searching the Internet for an answer to this question) and also with the values of MyKeyVal and type1 surrounded by escaped double quotes but neither way is working for me. I get errors stating “meta start tag on line 14 position 6 does not match the end tag of head line 22 position 3” but since the “data” variable doesn’t have any tags I’m not sure what should be changed to resolve such an error.
static string Submit(string ip, string id)
{
string response;
using (var client = new WebClient())
{
Uri uri = new System.Uri("https://" + ip + "/xxxxxxxxxx/" + id + "/XML");
string data = "key = MyKeyVal type = type1";
response = client.UploadString(uri, data);
}
//additional parsing of the response goes here
return response;
}