I will preface the code and explanation with the fact that I was able to run this code on the computers that my school has, and I have had multiple friends successfully run the code on their machines.
I have some C# code which is supposed to retrieve the RSS feed from a URL
public void GetRSS(string url)
{
// Connect to the web server
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
// Set the HTTP properties
wr.Timeout = 9000;
// Read the Response
WebResponse resp = wr.GetResponse();
Stream stream = resp.GetResponseStream();
// Load XML Document
XmlTextReader reader = new XmlTextReader(stream);
reader.XmlResolver = null;
xml = new XmlDocument();
xml.Load(reader);
//Select the "RSS items" from the feed
XmlNodeList items = xml.SelectNodes("//item");
//Open a serializer
XmlSerializer serializer =
new XmlSerializer(typeof(Rss));
using (XmlReader xreader = XmlReader.Create(new StringReader(xml.OuterXml)))
{
reader.MoveToContent();
rss = (Rss)new XmlSerializer(typeof(Rss)).Deserialize(xreader);
}
resp.Close();
}
This always throws "System.Net.WebException: The operation has timed out" on the line
WebResponse resp = wr.GetResponse();
My issue is that I cannot see why exactly this isn't working. I assume that no connection can be made to the URL, but no matter how many URLs I try, it never works. It just hangs for a few seconds and then comes up with the error.
To clarify, the program executes flawlessly on every machine I have tried it on aside from my home computer, on which I need to write the program.
Related
I encountered som issue when using Sharepoint LIst Source on an SSIS package (too many redirection...)
My goal is to retrieve (whatever the type of result (xml, rows...) the content of a sharepoint list based on a specific view.
I only have a http://xxxxxx/_vti_bin/Lists.asmx url access.
I currently try this kind of code:
string serviceURL = "https://xxxxx/_vti_bin/Lists.asmx";
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(serviceURL);
wr.UseDefaultCredentials = true;
wr.PreAuthenticate = true;
wr.Credentials = CredentialCache.DefaultCredentials;
wr.CookieContainer = new CookieContainer();
wr.AllowAutoRedirect = false;
// wr.MaximumAutomaticRedirections = 500;
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
// StreamReader readStream = new StreamReader(str, Encoding.UTF8);
string xmlData;
using (StreamReader sr = new StreamReader(str, Encoding.UTF8))
{
xmlData = sr.ReadToEnd();
sr.Close();
}
At this point it seems I have no longer error (too many redirection...) but now I'm block to get columns name and value, or xml value containing all data.
Using Web Service connected to the lists.asmx failed too, so I can't use this kind of solution.
Any ideas ?
thanks in advance.
regards,
I am currently developing in Unity (in particular using C#) and I'm stuck with HttpWebRequest - HttpWebResponse random timeouts.
I have some methods that send a POST request to a server I host on my local machine (XAMPP) to use various php scripts which are going to fetch informations from MySQL Database (hosted with XAMPP) and give back those info in JSON format.
Then I handle these JSON informations with my C# scripts.
The problem is that when I run the first test all is good:I can get the JSON data from my Server and show it in the Debug Console.
When I run the second test,a WebException is raised with error:
WebException - The request timed out
After that second test,if I run again and again,the problem keeps presenting in a random way.
I followed all the guidelines I found on the internet on how to setup a webrequest - webresponse properly,in particular I tried to use ServicePoint.DefaultConnectionLimit and ServicePoint.MaxServicePointIdleTime,without any result.
The general structure of my methods (regarding the web request/response part) is something like that:
public void WebMethod(){
string post_url = "http://localhost/service.php?someparam=1&someparam=2";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(post_url);
request.Method = "POST";
request.KeepAlive = false;
request.Timeout = 5000;
request.Proxy = null;
string Response = "";
try
{
using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
{
using (Stream objStream = resp.GetResponseStream())
{
using (StreamReader objReader = new StreamReader(objStream, Encoding.UTF8))
{
Response = objReader.ReadToEnd();
objReader.Close();
}
objStream.Flush();
objStream.Close();
}
resp.Close();
}
}catch(WebException e)
{
Debug.Log(e.Message);
}
finally
{
request.Abort();
}
//tried this one after reading some related answers here on StackOverflow,without results
//GC.Collect();
Debug.Log("SERVER RESPONSE:" + Response);
//Response Handling
}
I know that it may be something related to a wrong abort on the HttpWebRequest / Response or maybe related to the HTTP 1.1 connections limit,but I can't figure out any solution at the moment.
Any help is appreciated.
I'm trying to complete a PUT request to the IIS media services API - to try and set a publishing point to "stopped" state.
I've read the following link, which hasn't helped me very much!
https://msdn.microsoft.com/en-us/library/hh206014%28VS.90%29.aspx
My current code throws an exception on the the httpWebRequest1.GetResponse(), it indicates the web server is returning a 401 unauthorized error code:
string url = "http://localhost/LiveStream.isml/State";
var httpWebRequest1 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest1.ContentType = "application/atom+xml";
httpWebRequest1.Method = "PUT";
httpWebRequest1.Headers.Add("Authorization", "USERNAME:PASSWORD");
using (var streamWriter = new StreamWriter(httpWebRequest1.GetRequestStream()))
{
XmlDocument document = new XmlDocument();
document.Load("Resources/XMLFile1.xml");
string test = GetXMLAsString(document);
streamWriter.Write(test);
}
var httpResponse = (HttpWebResponse)httpWebRequest1.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
My Username/password was commented out, but they work fine when visiting the page in a browser, and inputting them in the username/password form that opens.
My Script essentially "PUT"s an XML document that is a copy of the XML document returned when visiting the state page in a browser.
Any help would be appreciated.
I am trying to get a response xml from the UIDAI data centers by sending an xml document using HttpWebRequest and receiving it using HttpWebResponse in C#ASP.net web based application. I already checked it using a java application and it is working fine on their side but the web based application is throwing an exception
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Note:The web exception occurs on the 5th line from the last
Please suggest some methods to resolve this
public void SendForAuthentication()//user defined function for Sending and Receiving Requests
{
string url = "http://auth.uidai.gov.in/1.6/public/9/9/MLTbKYcsgYMq1zgL3WMZYrnyvsarlljxpom2A-QTPc0Zud23shpnqPk";//the host address
StreamReader sr1 = new StreamReader("D:\\test-signed.xml");//loading xml file to stream
string XMLData = sr1.ReadToEnd();
string Conn = #"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>";
XMLData = Conn + XMLData;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(XMLData);
req.Method = "POST";
req.ContentType = "text/xml;charset=utf-8";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();//**here I am getting a webexception**
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd();
reader.ReadToFollowing("title");
File.WriteAllText("D:\\xml.xml", backstr);
}
edit:
The exception I get is "the operation has timed out" though I had increased the operation time
edit2:
The above code works fine there was actually a problem with my firewall
I am doing task of loading the live xml file (from live url) to XmlDataDocument, but every time I am getting error:
The operation has timed out
The code is as follows, The url containing the xml feeds , I want to load it into xmlDoc.
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load("http://www.globalgear.com.au/productfeed.xml");
Please suggest any solution.
Don't use the Load method of the XmlDataDocument class directly; you have little to no way of influencing the behaviour when it comes to long running HTTP requests.
Instead, use the HttpWebRequest and HttpWebResponse classes to do the work for you, and then load the subsequent response into your document.
For example:
HttpWebRequest rq = WebRequest.Create("http://www.globalgear.com.au/productfeed.xml") as HttpWebRequest;
//60 Second Timeout
rq.Timeout = 60000;
//Also note you can set the Proxy property here if required; sometimes it is, especially if you are behind a firewall - rq.Proxy = new WebProxy("proxy_address");
HttpWebResponse response = rq.GetResponse() as HttpWebResponse;
XmlTextReader reader = new XmlTextReader(response.GetResponseStream());
XmlDocument doc = new XmlDocument();
doc.Load(reader);
I've tested this code in a local app instance and the XmlDocument is populated with the data from your URL.
You can also substitute in XmlDataDocument for XmlDocument in the example above - I prefer to use XmlDocument as it's not (yet) marked as obsolete.
I've wrapped this in a function for you:
public XmlDocument GetDataFromUrl(string url)
{
XmlDocument urlData = new XmlDocument();
HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);
rq.Timeout = 60000;
HttpWebResponse response = rq.GetResponse() as HttpWebResponse;
using (Stream responseStream = response.GetResponseStream())
{
XmlTextReader reader = new XmlTextReader(responseStream);
urlData.Load(reader);
}
return urlData;
}
Simply call using:
XmlDocument document = GetDataFromUrl("http://www.globalgear.com.au/productfeed.xml");
To my knowledge there is no easy way to adjust the timeout with the method you are using.
The easiest change would be to use the webclient class and set the timeout property. This is described here http://w3ka.blogspot.co.uk/2009/12/how-to-fix-webclient-timeout-issue.html. Then use downloadfile on the webclient. Then load the saved file in the XMLDocument.
Set a timeout for your web request:
using System;
using System.Net;
using System.Xml;
namespace Shelver
{
class Program
{
static void Main(string[] args)
{
WebRequest requ = WebRequest.Create("http://www.globalgear.com.au/productfeed.xml");
requ.Timeout = 10 * 60 * 1000; // 10 minutes timeout and not 100s as the default.
var resp = requ.GetResponse();
Console.WriteLine("Will download {0:N0}bytes", resp.ContentLength);
var stream = resp.GetResponseStream();
XmlDocument doc = new XmlDocument();
doc.Load(stream);
}
}
}
This example will set it to 10 minutes.
In addition to the previous answers, which should be the first step towards fixing this, I continued to get this exception despite having already loaded the response and closing the connections.
The solution for me: the Load() and LoadXml() methods would throw their own Timeout exception if the value provided wasn't actually XML. Checking to verify that the response content was XML worked in our case (this will require that the host you are getting your response from actually sets content types).
Building upon dash's answer:
public XmlDocument GetDataFromUrl(string url)
{
XmlDocument urlData = new XmlDocument();
HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);
rq.Timeout = 60000;
HttpWebResponse response = rq.GetResponse() as HttpWebResponse;
// New check added to dash's answer.
if (response.ContentType.Contains("text/xml")
{
using (Stream responseStream = response.GetResponseStream())
{
XmlTextReader reader = new XmlTextReader(responseStream);
urlData.Load(reader);
}
}
return urlData;
}