Pass through XML from another website - c#

I am trying to pass through some XML from an external website.
What is the best way of doing this, through c# webpage or asp.MVC?

I tend to use something like this for working with external XML documents / RSS feeds etc:
string sURL = ".....";
// Create a request for the URL.
WebRequest oRequest = WebRequest.Create(sUrl);
// Get the response.
WebResponse oResponse = oRequest.GetResponse();
// Get the stream containing content returned by the server.
Stream oDataStream = oResponse.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader oReader = new StreamReader(oDataStream, System.Text.Encoding.Default);
// Read the content.
string sXML = oReader.ReadToEnd();
// Convert string to XML
XDocument oFeed = XDocument.Parse(sXML);

Either should be fine. MVC is probably easiest (in terms of getting a raw response), but you could do the same in regular ASP.NET just by using a handler (possibly .ashx), or just by clearing the response.

Related

How can I get the embed data from PDFviewer page with C# or JS?

I'm creating some test with selenium and C# .I have a window with PDF Viewer, I want to read the HTTP header of this window but the URL that is showing in the window is not what I'm supposed to use to get this data.
Honestly I don't really understand how is encoding and decoding this files. The thing that I want is read the filename to get the file then read it, and be sure that doesn't contain errors.
Some advises or links for understand better HTTP headers, metafiles, how enconde and decode pdf files etc.
Here is some of my code
URL url = new URL(driver.Url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.toString());
try
{
HttpWebResponse res = (HttpWebResponse)request.GetResponse();
using (Stream rstream = res.GetResponseStream())
{
string fileName = res.Headers["Content-Disposition"] ;
}
res.Close();
}
catch{
}

How to read javascript file using HttpWebRequest in c#

I have a javascript file in a remote server, and when I use httpwebrequest it returns some weird characters.
Thr url is http://goo.gl/0Ug5QI
Is this kind of compressed contents?
static string GetScriptSource(string _url)
{
string _retValue = string.Empty;
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(_url);
hwr.Method = "GET";
HttpWebResponse res = (HttpWebResponse)hwr.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
My code to read that script file's content is very simple.
Looking at the js source that you linked to, it could be that is has been gzipped. Try saving the source as a file and use 7zip or something to see if you can unzip it. There is a GZip library in C# so if it has been gzipped then you should be able to unzip it easily enough.
Although it's a Korean web site so maybe the encoding is not correct.
Either way it's not a problem with the code that you posted.

Parsing XML in a C# Application?

Right now, I am getting a Google search's XML. However, the XML doc is so big, I can't find anything anywhere. I am wondering how I can find the answer on Google. By that, I mean when you Google "Capital of Florida" the box at the top says Tallahassee. I want to access that information but I am unsure how.
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var response = request.GetResponse();
var rstream = response.GetResponseStream();
var sr = new StreamReader(rstream);
var json = sr.ReadToEnd();
Console.WriteLine(json.ToString());
The last Console.Writeline obviously just shoots out a huge monster of an XML doc
See this it uses LINQ to extract a piece of info from XML documents https://coderwall.com/p/qghcqw
if you are requesting HTML, a good way to parse the data is using HtmlAgilityPack
http://htmlagilitypack.codeplex.com/

How to Get Files From A Directory That's Open for Directory Listing?

Assume that a website has set the directory listing permissions open for one of its folder and I can see files on the web browser[as on the image]. Is there a way to get files in this folder to an array?
In a word, I need to use a method that's doing what DirectoryInfo.GetFiles("C:/"); for an URL.
Edit: I'd rather want to use a method instead of getting web response and parse the result. That's like a back door in case there's not an alternative way.
This code will help you get the html
WebRequest request = WebRequest.Create("http://yourwebsite/yourwebdirectory/");
var webResponse=request.GetResponse();
Stream dataStream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
webResponse.Close();
Once you get the html you can parse it to get the file names

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%3CCurrencyCode%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);

Categories

Resources