我在C#程序中编写了一个方法。 这是代码:
private string connect(string mypage, string myrln, string myusername , string mypassword)
{
WebRequest request = WebRequest.Create("http://localhost/vpn/"+mypage+".asp?prog=y&u="+myusername+"&p="+mypassword+"&rln="+myrln);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
我第一次称这种方法,一切正常。 但是第二次,(500)内部服务器错误提升。 我该如何解决这个问题?
错误在这一行:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
答案 0 :(得分:1)
服务器上发生错误(很可能是在.ASP页面中)。将日志记录添加到服务器页面,以便了解.asp页面中发生的情况。
此外,在代码周围使用它以确保在抛出异常时处理它。
WebRequest request = WebRequest.Create("http://localhost/vpn/"+mypage+".asp?prog=y&u="+myusername+"&p="+mypassword+"&rln="+myrln);
request.Credentials = CredentialCache.DefaultCredentials;
string responseFromServer;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream dataStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(dataStream))
{
responseFromServer = reader.ReadToEnd();
}