So I sent an HTTPWebRequest using application/x-www-form-urlencoded as my content type. I assume that this means the reponse will be returned in a similar type? (EDIT: Have now been told this isn't the case)
My question is this. How do I access the different key/value pairs returned in the response. My code so far looks like this. I can of course read the string but surely there is a better way to access the data other than ripping the string apart.
HttpWebResponse response = SendPOSTRequest("https://site/page?Service=foo", content.ToString(), "", "", true);
string responseCode = response.StatusCode.ToString();
string responseStatusDescription = response.StatusDescription;
StreamReader sr = new StreamReader(response.GetResponseStream());
string result = sr.ReadToEnd();
I tried using XML/linq to read the elements into an XDocument but of course it is not being returned in XML form.
Assume I have 3 or 4 different pieces of information in there how could I read them out?
EDIT: I have just checked and the data is being returned as text/plain. How can this be processed easily?
EDIT: The response string once retrieved via a streamreader is:
VPSProtocol=2.23
Status=OK
StatusDetail=Server transaction registered successfully.
VPSTxId={FDC93F3D-FC64-400D-875F-0B7E855AD81F}
SecurityKey=*****
NextURL=https://foo.com/PaymentPage.asp?TransactionID={875F-0B7E855AD81F}
EDIT: It seems this piece of code gets me part way there. I am able to break the response up into a List and address it line by line. It still isnt a perfect solution though.
StreamReader sr = new StreamReader(response.GetResponseStream());
List<string> str = new List<string>;
while (sr.Peek() >= 0)
{
str.Add(sr.ReadLine().ToString());
}
This is a bit quick and dirty, but you could just parse it out to a Dictionary like this;
var parsed = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None)
.Select(x =>
{
int split = x.IndexOf('=');
return new
{
Key = x.Substring(0, split),
Value = x.Substring(split + 1, x.Length - (split + 1))
};
}).ToDictionary(k => k.Key, v => v.Value);
Not sure how robust this is, but it should be a good start.
Edit: Updated to handle '=' character in the value.
So my case was a bit specialised in that I always know what I am going to have to deal with if the response does not contain certain words such as INVALID or MALFORMED. A dirty fix for me to get the other data from the text/plain response was achieved like so:
string responseCode = response.StatusCode.ToString();
string responseStatusDescription = response.StatusDescription;
StreamReader sr = new StreamReader(response.GetResponseStream());
string result = sr.ReadToEnd();
string[] parsed = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> str = parsed.ToList();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (string s in str)
{
string[] ss = s.Split('=');
if (ss.Count() == 2)
{
dictionary.Add(ss[0], ss[1]);
}
else
{
string value = "";
for (int i = 0; i < ss.Count(); i++)
{
switch (i)
{
case 0:
{
break;
}
case 1:
{
value += ss[i];
break;
}
default:
{
value += "=" + ss[i];
break;
}
}
}
dictionary.Add(ss[0], value);
}
}
The switch statement was introduced because in the event of having a url passed back with a series of querystrings it was important to reconstruct them. The code above works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.