By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

WebRequest.GetResponse() throwing exception (Internal Server error

P: n/a
Hello,

I am trying to get a response for an .aspx page in my current project (same
virtual directory) by using WebRequest.GetResponse but I keep getting a
exception with "500 Internal server error" in the exception message.

I am able to do this fine with another .aspx page that has no code-behind.
The page that has code-behind throws the exception.

What I am doing is getting the .aspx response, reading the stream, replacing
pre-determined tags with app data, and then sending the HTML as an email
body, but I only have problems getting the response. The other logic works
fine (based on experience with the other, working page).

I am trying to get the response from a page that has server label controls
whose text is set by the code-behind depending on the culture set in the web
app.

I'm quite confused as I thought I should be able to call
WebRequest.GetResponse on any page that I can load manually in Internet
Explorer (which, in this case, is true).

Here is the code in question:

System.Net.WebRequest req = System.Net.WebRequest.Create(applicationRootURL
+ ResponseEmailSourceFileName);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.WebResponse resp = req.GetResponse();
The page's code-behind is:

public class ResponseEmailTemplate : InitializedThreadCulturePage
{
private const string BaseResourceName = "ResponseEmailTemplate";

protected System.Web.UI.WebControls.Label lblPartiesInvolved;
protected System.Web.UI.WebControls.Label lblAmountClaimed;
protected System.Web.UI.WebControls.Label lblMatterNumber;
protected System.Web.UI.WebControls.Label lblRespondingLawyer;
protected System.Web.UI.WebControls.Label lblDescription;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
LocalizePage();
}

private void LocalizePage()
{
ResourceManager rsm = this.GetPageResourceManager(BaseResourceName);

// Localize labels.
lblPartiesInvolved.Text = rsm.GetString("Label.PartiesInvolved");
lblAmountClaimed.Text = rsm.GetString("Label.AmountClaimed");
lblMatterNumber.Text = rsm.GetString("Label.MatterNumber");
lblRespondingLawyer.Text = rsm.GetString("Label.RespondingLawyer");
lblDescription.Text = rsm.GetString("Label.Description");
}

Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Does the page depend on any sort of client header being sent to indicate
the locale?

You might want to compare what the browser is sending over HTTP versus your
program with a tool like Fiddler [1]. These problems usually come down to
different headers or POST values.

[1] http://www.fiddlertool.com/fiddler/

--
Scott
http://www.OdeToCode.com/blogs/scott/
Hello,

I am trying to get a response for an .aspx page in my current project
(same virtual directory) by using WebRequest.GetResponse but I keep
getting a exception with "500 Internal server error" in the exception
message.

I am able to do this fine with another .aspx page that has no
code-behind. The page that has code-behind throws the exception.

What I am doing is getting the .aspx response, reading the stream,
replacing pre-determined tags with app data, and then sending the HTML
as an email body, but I only have problems getting the response. The
other logic works fine (based on experience with the other, working
page).

I am trying to get the response from a page that has server label
controls whose text is set by the code-behind depending on the culture
set in the web app.

I'm quite confused as I thought I should be able to call
WebRequest.GetResponse on any page that I can load manually in
Internet Explorer (which, in this case, is true).

Here is the code in question:

System.Net.WebRequest req =
System.Net.WebRequest.Create(applicationRootURL
+ ResponseEmailSourceFileName);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.WebResponse resp = req.GetResponse();
The page's code-behind is:

public class ResponseEmailTemplate : InitializedThreadCulturePage
{
private const string BaseResourceName = "ResponseEmailTemplate";
protected System.Web.UI.WebControls.Label lblPartiesInvolved;
protected System.Web.UI.WebControls.Label lblAmountClaimed;
protected System.Web.UI.WebControls.Label lblMatterNumber;
protected System.Web.UI.WebControls.Label lblRespondingLawyer;
protected System.Web.UI.WebControls.Label lblDescription;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
LocalizePage();
}
private void LocalizePage()
{
ResourceManager rsm =
this.GetPageResourceManager(BaseResourceName);
// Localize labels.
lblPartiesInvolved.Text = rsm.GetString("Label.PartiesInvolved");
lblAmountClaimed.Text = rsm.GetString("Label.AmountClaimed");
lblMatterNumber.Text = rsm.GetString("Label.MatterNumber");
lblRespondingLawyer.Text = rsm.GetString("Label.RespondingLawyer");
lblDescription.Text = rsm.GetString("Label.Description");
}

Nov 19 '05 #2

P: n/a
THANK YOU! That's a great little tool!

Now, I am running into an issue trying to use it with VS.NET 2003. When I
debug the web app and get a couple levels deep in the navigation, I get an
error saying that I have too many connections to the web server. (HTTP 403.9
- Access Forbidden: Too many users are connected Internet Information
Services). Sometimes I get an error directly from Fiddler about too many
socket connections as well. I found mention of this on the Fiddler forum
here: http://www.bayden.com/bbs/viewtopic.php?t=253

I have a corporate ISA proxy that requires NTLM authentication. I am using
Fiddler's "Require Authentication", but after reading the forums, it looks as
though I won't be able to use Fiddler because of NTLM authentication hop
limitations. Is this correct?

I tried the setting suggested on the blog
(System.Net.GlobalProxySelection.Select = new
System.Net.WebProxy("127.0.0.1", 8888);) but Fiddler never seems to catch any
requests when I'm debugging in VS.NET (although the request goes through just
fine. I did see mention of this in the forum as well).

Anyway, in all of my messing around with Fiddler, I was able to pinpoint the
problem as the proxy settings in my WebRequest.

Thanks again for the tool suggestion!

"Scott Allen" wrote:
Does the page depend on any sort of client header being sent to indicate
the locale?

You might want to compare what the browser is sending over HTTP versus your
program with a tool like Fiddler [1]. These problems usually come down to
different headers or POST values.

[1] http://www.fiddlertool.com/fiddler/

--
Scott
http://www.OdeToCode.com/blogs/scott/
Hello,

I am trying to get a response for an .aspx page in my current project
(same virtual directory) by using WebRequest.GetResponse but I keep
getting a exception with "500 Internal server error" in the exception
message.

I am able to do this fine with another .aspx page that has no
code-behind. The page that has code-behind throws the exception.

What I am doing is getting the .aspx response, reading the stream,
replacing pre-determined tags with app data, and then sending the HTML
as an email body, but I only have problems getting the response. The
other logic works fine (based on experience with the other, working
page).

I am trying to get the response from a page that has server label
controls whose text is set by the code-behind depending on the culture
set in the web app.

I'm quite confused as I thought I should be able to call
WebRequest.GetResponse on any page that I can load manually in
Internet Explorer (which, in this case, is true).

Here is the code in question:

System.Net.WebRequest req =
System.Net.WebRequest.Create(applicationRootURL
+ ResponseEmailSourceFileName);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.WebResponse resp = req.GetResponse();
The page's code-behind is:

public class ResponseEmailTemplate : InitializedThreadCulturePage
{
private const string BaseResourceName = "ResponseEmailTemplate";
protected System.Web.UI.WebControls.Label lblPartiesInvolved;
protected System.Web.UI.WebControls.Label lblAmountClaimed;
protected System.Web.UI.WebControls.Label lblMatterNumber;
protected System.Web.UI.WebControls.Label lblRespondingLawyer;
protected System.Web.UI.WebControls.Label lblDescription;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
LocalizePage();
}
private void LocalizePage()
{
ResourceManager rsm =
this.GetPageResourceManager(BaseResourceName);
// Localize labels.
lblPartiesInvolved.Text = rsm.GetString("Label.PartiesInvolved");
lblAmountClaimed.Text = rsm.GetString("Label.AmountClaimed");
lblMatterNumber.Text = rsm.GetString("Label.MatterNumber");
lblRespondingLawyer.Text = rsm.GetString("Label.RespondingLawyer");
lblDescription.Text = rsm.GetString("Label.Description");
}


Nov 19 '05 #3

P: n/a
I thought I'd post an update on my issue. It turns out that my problem had
nothing to do with the proxy settings.

I inherit my aspx page from a base class of type
"InitializedThreadCulturePage". This is a class (derived from
System.Web.UI.Page) that sets up the Thread Culture settings depending on a
user language choice (stored in a cookie).

I tried my code with a brand new "WebForm1.aspx" with no code in the
code-behind and had no problems. The next thing I tried was to inherit this
new WebForm1 from "InitializedThreadCulturePage" and I received the 500
response code.

Here is the page code:

using System;
using System.Resources;

namespace MyPage.Web.UI
{
/// <summary>
/// Base class for other pages in this application that require the
/// CultureInfo to be set on the current thread. Pages should
/// inherit from this class if they require this functionality.
///
/// This class also provides a method to retrieve the ResourceManager
/// for a particular page. In order to use this functionality, users
/// must create a resource file with the same filename as the web page,
/// minus the .aspx extension. Thus, if a page is named WebForm.aspx,
/// the user would create a file in the Resources folder called
/// WebForm.resx. Then, when you call the GetPageResourceManager
/// method, simply pass in the name WebForm in order to get the
/// appropriate resource set.
/// </summary>
public class InitializedThreadCulturePage : System.Web.UI.Page
{
private ResourceManager m_pageResourceManager;

/// <summary>
/// Constructor that adds an event handler for the page's Load
/// event. The event handler will initialize the current thread's
/// CultureInfo appropriately.
/// </summary>
public InitializedThreadCulturePage()
{
this.Load += new System.EventHandler(this.Page_Load);
}

/// <summary>
/// Returns the page's resource manager. The baseName parameter
/// should match the name of the resource file in the Resources
/// directory. That is, if a resource exists named WebForm.resx,
/// the parameter supplied should be "WebForm".
/// </summary>
/// <remarks>
/// This method assumes that all resources are stored in satellite
/// assemblies with the neutral resources residing in the currently
/// executing assembly. Also, this method assumes that all resource
/// files are placed in the Resources directory before compilation.
/// </remarks>
/// <param name="baseName">
/// The name of the resource set to retrieve. This name will match
/// the name of the resource file in the Resources directory.
/// </param>
/// <returns>
/// A ResourceManager object containing the specified resource set.
/// </returns>
protected ResourceManager GetPageResourceManager(string baseName)
{
if (m_pageResourceManager == null)
{
m_pageResourceManager = new ResourceManager(
"MyPage.Web.UI.Resources." + baseName,
System.Reflection.Assembly.GetExecutingAssembly()) ;
}

return m_pageResourceManager;
}

// This event handler will initialize the current thread's CultureInfo
// by looking for a cookie in the page that specifies the desired locale.
// If this cookie exists, the CultureInfo will be created using that
// locale. If not, then this event handler will retrieve the locale
// specified by in the HTTP header and use that to create the CultureInfo
// object.
// As a side effect, if a cookie exists, the cookie's expiration date
// will be reset.
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.HttpCookie languageCookie =
Request.Cookies[WebConstants.SelectedLanguageParameterName];

string locale = Request.UserLanguages[0];
if (languageCookie != null)
{
locale = languageCookie.Value;

// Update the expiry date on the cookie.
languageCookie.Expires =
DateTime.Now.AddYears(WebConstants.CookieExpiratio nLength);
}

// Set the culture info for the current thread.
System.Threading.Thread.CurrentThread.CurrentUICul ture =
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture(locale);

if (languageCookie != null)
{
Response.Cookies.Add(languageCookie);
}
}
}
}

Any ideas as to why this doesn't work when creating a WebRequest
programmatically?

Thanks in advance.

"Terry" wrote:
THANK YOU! That's a great little tool!

Now, I am running into an issue trying to use it with VS.NET 2003. When I
debug the web app and get a couple levels deep in the navigation, I get an
error saying that I have too many connections to the web server. (HTTP 403.9
- Access Forbidden: Too many users are connected Internet Information
Services). Sometimes I get an error directly from Fiddler about too many
socket connections as well. I found mention of this on the Fiddler forum
here: http://www.bayden.com/bbs/viewtopic.php?t=253

I have a corporate ISA proxy that requires NTLM authentication. I am using
Fiddler's "Require Authentication", but after reading the forums, it looks as
though I won't be able to use Fiddler because of NTLM authentication hop
limitations. Is this correct?

I tried the setting suggested on the blog
(System.Net.GlobalProxySelection.Select = new
System.Net.WebProxy("127.0.0.1", 8888);) but Fiddler never seems to catch any
requests when I'm debugging in VS.NET (although the request goes through just
fine. I did see mention of this in the forum as well).

Anyway, in all of my messing around with Fiddler, I was able to pinpoint the
problem as the proxy settings in my WebRequest.

Thanks again for the tool suggestion!

"Scott Allen" wrote:
Does the page depend on any sort of client header being sent to indicate
the locale?

You might want to compare what the browser is sending over HTTP versus your
program with a tool like Fiddler [1]. These problems usually come down to
different headers or POST values.

[1] http://www.fiddlertool.com/fiddler/

--
Scott
http://www.OdeToCode.com/blogs/scott/
Hello,

I am trying to get a response for an .aspx page in my current project
(same virtual directory) by using WebRequest.GetResponse but I keep
getting a exception with "500 Internal server error" in the exception
message.

I am able to do this fine with another .aspx page that has no
code-behind. The page that has code-behind throws the exception.

What I am doing is getting the .aspx response, reading the stream,
replacing pre-determined tags with app data, and then sending the HTML
as an email body, but I only have problems getting the response. The
other logic works fine (based on experience with the other, working
page).

I am trying to get the response from a page that has server label
controls whose text is set by the code-behind depending on the culture
set in the web app.

I'm quite confused as I thought I should be able to call
WebRequest.GetResponse on any page that I can load manually in
Internet Explorer (which, in this case, is true).

Here is the code in question:

System.Net.WebRequest req =
System.Net.WebRequest.Create(applicationRootURL
+ ResponseEmailSourceFileName);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.WebResponse resp = req.GetResponse();
The page's code-behind is:

public class ResponseEmailTemplate : InitializedThreadCulturePage
{
private const string BaseResourceName = "ResponseEmailTemplate";
protected System.Web.UI.WebControls.Label lblPartiesInvolved;
protected System.Web.UI.WebControls.Label lblAmountClaimed;
protected System.Web.UI.WebControls.Label lblMatterNumber;
protected System.Web.UI.WebControls.Label lblRespondingLawyer;
protected System.Web.UI.WebControls.Label lblDescription;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
LocalizePage();
}
private void LocalizePage()
{
ResourceManager rsm =
this.GetPageResourceManager(BaseResourceName);
// Localize labels.
lblPartiesInvolved.Text = rsm.GetString("Label.PartiesInvolved");
lblAmountClaimed.Text = rsm.GetString("Label.AmountClaimed");
lblMatterNumber.Text = rsm.GetString("Label.MatterNumber");
lblRespondingLawyer.Text = rsm.GetString("Label.RespondingLawyer");
lblDescription.Text = rsm.GetString("Label.Description");
}


Nov 19 '05 #4

P: n/a
I doubt anyone is even still noticing this thread, but, for the sake of
consistency, I thought I'd share the solution to the problem that I was
having.

It turns out that the base class on which I was deriving all of my other
pages threw an exception in the Page_Load when trying to access
Request.UserLanguages[0]. This presented itself only when programmatically
requesting a page because this property was null.

Simple as that. No proxy issues, just poor code. :)

"Terry" wrote:
I thought I'd post an update on my issue. It turns out that my problem had
nothing to do with the proxy settings.

I inherit my aspx page from a base class of type
"InitializedThreadCulturePage". This is a class (derived from
System.Web.UI.Page) that sets up the Thread Culture settings depending on a
user language choice (stored in a cookie).

I tried my code with a brand new "WebForm1.aspx" with no code in the
code-behind and had no problems. The next thing I tried was to inherit this
new WebForm1 from "InitializedThreadCulturePage" and I received the 500
response code.

Here is the page code:

using System;
using System.Resources;

namespace MyPage.Web.UI
{
/// <summary>
/// Base class for other pages in this application that require the
/// CultureInfo to be set on the current thread. Pages should
/// inherit from this class if they require this functionality.
///
/// This class also provides a method to retrieve the ResourceManager
/// for a particular page. In order to use this functionality, users
/// must create a resource file with the same filename as the web page,
/// minus the .aspx extension. Thus, if a page is named WebForm.aspx,
/// the user would create a file in the Resources folder called
/// WebForm.resx. Then, when you call the GetPageResourceManager
/// method, simply pass in the name WebForm in order to get the
/// appropriate resource set.
/// </summary>
public class InitializedThreadCulturePage : System.Web.UI.Page
{
private ResourceManager m_pageResourceManager;

/// <summary>
/// Constructor that adds an event handler for the page's Load
/// event. The event handler will initialize the current thread's
/// CultureInfo appropriately.
/// </summary>
public InitializedThreadCulturePage()
{
this.Load += new System.EventHandler(this.Page_Load);
}

/// <summary>
/// Returns the page's resource manager. The baseName parameter
/// should match the name of the resource file in the Resources
/// directory. That is, if a resource exists named WebForm.resx,
/// the parameter supplied should be "WebForm".
/// </summary>
/// <remarks>
/// This method assumes that all resources are stored in satellite
/// assemblies with the neutral resources residing in the currently
/// executing assembly. Also, this method assumes that all resource
/// files are placed in the Resources directory before compilation.
/// </remarks>
/// <param name="baseName">
/// The name of the resource set to retrieve. This name will match
/// the name of the resource file in the Resources directory.
/// </param>
/// <returns>
/// A ResourceManager object containing the specified resource set.
/// </returns>
protected ResourceManager GetPageResourceManager(string baseName)
{
if (m_pageResourceManager == null)
{
m_pageResourceManager = new ResourceManager(
"MyPage.Web.UI.Resources." + baseName,
System.Reflection.Assembly.GetExecutingAssembly()) ;
}

return m_pageResourceManager;
}

// This event handler will initialize the current thread's CultureInfo
// by looking for a cookie in the page that specifies the desired locale.
// If this cookie exists, the CultureInfo will be created using that
// locale. If not, then this event handler will retrieve the locale
// specified by in the HTTP header and use that to create the CultureInfo
// object.
// As a side effect, if a cookie exists, the cookie's expiration date
// will be reset.
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.HttpCookie languageCookie =
Request.Cookies[WebConstants.SelectedLanguageParameterName];

string locale = Request.UserLanguages[0];
if (languageCookie != null)
{
locale = languageCookie.Value;

// Update the expiry date on the cookie.
languageCookie.Expires =
DateTime.Now.AddYears(WebConstants.CookieExpiratio nLength);
}

// Set the culture info for the current thread.
System.Threading.Thread.CurrentThread.CurrentUICul ture =
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture(locale);

if (languageCookie != null)
{
Response.Cookies.Add(languageCookie);
}
}
}
}

Any ideas as to why this doesn't work when creating a WebRequest
programmatically?

Thanks in advance.

"Terry" wrote:
THANK YOU! That's a great little tool!

Now, I am running into an issue trying to use it with VS.NET 2003. When I
debug the web app and get a couple levels deep in the navigation, I get an
error saying that I have too many connections to the web server. (HTTP 403.9
- Access Forbidden: Too many users are connected Internet Information
Services). Sometimes I get an error directly from Fiddler about too many
socket connections as well. I found mention of this on the Fiddler forum
here: http://www.bayden.com/bbs/viewtopic.php?t=253

I have a corporate ISA proxy that requires NTLM authentication. I am using
Fiddler's "Require Authentication", but after reading the forums, it looks as
though I won't be able to use Fiddler because of NTLM authentication hop
limitations. Is this correct?

I tried the setting suggested on the blog
(System.Net.GlobalProxySelection.Select = new
System.Net.WebProxy("127.0.0.1", 8888);) but Fiddler never seems to catch any
requests when I'm debugging in VS.NET (although the request goes through just
fine. I did see mention of this in the forum as well).

Anyway, in all of my messing around with Fiddler, I was able to pinpoint the
problem as the proxy settings in my WebRequest.

Thanks again for the tool suggestion!

"Scott Allen" wrote:
Does the page depend on any sort of client header being sent to indicate
the locale?

You might want to compare what the browser is sending over HTTP versus your
program with a tool like Fiddler [1]. These problems usually come down to
different headers or POST values.

[1] http://www.fiddlertool.com/fiddler/

--
Scott
http://www.OdeToCode.com/blogs/scott/

> Hello,
>
> I am trying to get a response for an .aspx page in my current project
> (same virtual directory) by using WebRequest.GetResponse but I keep
> getting a exception with "500 Internal server error" in the exception
> message.
>
> I am able to do this fine with another .aspx page that has no
> code-behind. The page that has code-behind throws the exception.
>
> What I am doing is getting the .aspx response, reading the stream,
> replacing pre-determined tags with app data, and then sending the HTML
> as an email body, but I only have problems getting the response. The
> other logic works fine (based on experience with the other, working
> page).
>
> I am trying to get the response from a page that has server label
> controls whose text is set by the code-behind depending on the culture
> set in the web app.
>
> I'm quite confused as I thought I should be able to call
> WebRequest.GetResponse on any page that I can load manually in
> Internet Explorer (which, in this case, is true).
>
> Here is the code in question:
>
> System.Net.WebRequest req =
> System.Net.WebRequest.Create(applicationRootURL
> + ResponseEmailSourceFileName);
> req.Credentials = System.Net.CredentialCache.DefaultCredentials;
> System.Net.WebResponse resp = req.GetResponse();
> The page's code-behind is:
>
> public class ResponseEmailTemplate : InitializedThreadCulturePage
> {
> private const string BaseResourceName = "ResponseEmailTemplate";
> protected System.Web.UI.WebControls.Label lblPartiesInvolved;
> protected System.Web.UI.WebControls.Label lblAmountClaimed;
> protected System.Web.UI.WebControls.Label lblMatterNumber;
> protected System.Web.UI.WebControls.Label lblRespondingLawyer;
> protected System.Web.UI.WebControls.Label lblDescription;
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> LocalizePage();
> }
> private void LocalizePage()
> {
> ResourceManager rsm =
> this.GetPageResourceManager(BaseResourceName);
> // Localize labels.
> lblPartiesInvolved.Text = rsm.GetString("Label.PartiesInvolved");
> lblAmountClaimed.Text = rsm.GetString("Label.AmountClaimed");
> lblMatterNumber.Text = rsm.GetString("Label.MatterNumber");
> lblRespondingLawyer.Text = rsm.GetString("Label.RespondingLawyer");
> lblDescription.Text = rsm.GetString("Label.Description");
> }

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.