如何解决从URL读取数据时发生的服务器错误(500)。如何获取哪种类型的服务器错误?
大家好,
我想以xml格式从URL读取数据。
那个是代码,
Hi everyone,
I want to read data from URL in xml format.
That is the code,
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);
代码xmldoc.LoadXml(网站) ;给出上面的错误
System.Xml.dll中发生未处理的System.Net.WebException类型异常
附加信息:远程服务器返回了一个错误:(500)内部服务器错误。
我用Google搜索并了解到有可能出现这种情况的原因服务器错误。我找到了Fiddler来找出我遇到的服务器错误。但是,当我运行代码问题时,结果= 500并且没有给出日志。
因此,我想了解System.Xml.dll。
这就是问题,我的目的是从URL读取数据
感谢您的建议
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
尝试像bellow
这是一个样本。
...
public static String excutePost(String targetURL,String urlParameters)
{
URL url;
HttpURLConnection connection =空值;
尝试{
//创建连接
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(真的);
//发送请求
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream() );
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
//获取响应
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is) ));
字符串行;
StringBuffer response = new StringBuffer();
while((line = rd.readLine())!= null){
response.append(line);
response.append ('\ r');
}
rd.close();
返回response.toString();
} catch(例外e){
e.printStackTrace();
返回null;
}终于{
if(connection!= null){
connection.disconnect();
}
}
}
...
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();
}
}
}
...