How do I do a postback to a CGI service
I am building a C# client for a CGI service (not sure what its called exactly).
It accepts a bunch of XML and spits out a response. I have tested it straight in Firefox and it works (see below).
Now I am not sure how to do this in C# code though? Does anyone have a helpful snippet of code, I can't imagine it would be that difficult?
http://www.travelcommunications.co.uk/cgi-bin/d3web_gzip.ssh?%3CTCOML%20version=%22NEWFORMAT%22%3E%3CTransferOnly%3E%3CAvailability%3E%3CRequest%3E%3CAgentCode%3ETEST%3C/AgentCode%3E%3CAgentType%3ETA%3C/AgentType%3E%3CDeparturePointCode%3EALC%3C/DeparturePointCode%3E%3CDeparturePointType%3EAIRPORT%3C/DeparturePointType%3E%3CArrivalPointCode%3EBEN%3C/ArrivalPointCode%3E%3CArrivalPointType%3ERESORT%3C/ArrivalPointType%3E%3CSectorType%3ERETURN%3C/SectorType%3E%3CArrDate%3E10.10.10%3C/ArrDate%3E%3CArrTime%3E10:00%3C/ArrTime%3E%3CRetDate%3E17.10.10%3C/RetDate%3E%3CRetTime%3E10:00%3C/RetTime%3E%3CBrochure%3E001%3C/Brochure%3E%3CAdults%3E2%3C/Adults%3E%3CChildren%3E0%3C/Children%3E%3CInfants%3E0%3C/Infants%3E%3CCurr开发者_Python百科encyCode%3EUKL%3C/CurrencyCode%3E%3C/Request%3E%3C/Availability%3E%3C/TransferOnly%3E%3C/TCOML%3E
You're looking for the WebClient
class.
For example: (2nd EDIT: With GZIP; this code is tested and actually works)
string response;
using (var client = new WebClient()) {
byte[] bytes = client.DownloadData(url);
using(var reader = new StreamReader(new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress)))
response = reader.ReadToEnd();
}
However, if the URL returns raw XML, you can also load the XML directly from the URL, like this:
var doc = XDocument.Load(url);
精彩评论