Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi everyone,

I want to read data from URL in xml format.

That is the code,

C#
WebProxy wb = new WebProxy(WebProxy.GetDefaultProxy().Address);

            WebClient wc = new WebClient();
            wc.Proxy = wb;
            string website = wc.DownloadString("http://www.mgm.gov.tr/tahmin/il-ve-ilceler.aspx?m=ISTANBUL/");

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(website);



The code xmldoc.LoadXml(website); gives the error above

An unhandled exception of type 'System.Net.WebException' occurred in System.Xml.dll
Additional information: The remote server returned an error: (500) Internal Server Error.


I googled and learned that there can be severel reasons for that server error. I found Fiddler to find out which server error i faced. But when i run the code the problem just says, Result = 500 and gives no log.

Therefore, I chechked the System.Xml.dll.

That is the issue, i just aimed to read data from URL
Thanks for advice
Posted
Updated 23-Oct-14 20:09pm
v4
Comments
Richard MacCutchan 23-Oct-14 4:14am    
You need to contact the administrators at the website you are trying to access.
denegabze 23-Oct-14 4:22am    
I want to get weather conditions from meteorology that is managed by state. ı am not sure they will help me
Richard MacCutchan 23-Oct-14 4:57am    
Why would they not help you? The error message is clear, internal server error, so it is their site that has the problem.
denegabze 23-Oct-14 4:24am    
if there is any alternative solution could you please recommend
Richard MacCutchan 23-Oct-14 5:03am    
If I type that URL into my browser it returns an HTML web page, not an XML document. Are you sure that the address is valid?

1 solution

try like bellow

Here is one sample.

...
public static String excutePost(String targetURL, String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");

connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();

//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();

} catch (Exception e) {

e.printStackTrace();
return null;

} finally {

if(connection != null) {
connection.disconnect();
}
}
}
...
 
Share this answer
 
Comments
Richard MacCutchan 24-Oct-14 4:21am    
Please format your code properly, so it is readable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900