Problem HttpWebRequest from asp.net app

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I created a class to submit an xml document to a webpage.

When I use my class in a console application, everything submits correctly.

However, when I attempt to use my class from an asp.net webpage, then it
always gives me this exception
The underlying connection was closed: Unable to connect to the remote server.

I am using the System.Net namespace
Code is below:

I always get the exception on this line:
using (Stream requestStream = request.GetRequestStream())

Thanks in advance for any hints. tips to solve this problem

public string SubmitIRAlerts(string url)
{
string returnValue;
StreamReader sr;
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bytes = enc.GetBytes(this.XmlData);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream())
// always produces error on code above
{
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
try
{
if (response.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
sr = new StreamReader(response.GetResponseStream());
try
{
returnValue = sr.ReadToEnd();
}
finally
{
if ( sr != null )
sr.Close();
}
}
finally
{
if ( response != null )
response.Close();
}
return returnValue;
}
 
Thus wrote Darren,
I created a class to submit an xml document to a webpage.

When I use my class in a console application, everything submits
correctly.

However, when I attempt to use my class from an asp.net webpage, then
it
always gives me this exception
The underlying connection was closed: Unable to connect to the remote
server.
I am using the System.Net namespace
Code is below:
I always get the exception on this line:
using (Stream requestStream = request.GetRequestStream())
Thanks in advance for any hints. tips to solve this problem

Just to be sure... you also get the error when running the ASP.NET page on
your local machine, on which the console application works?

Cheers,
 
I only get the error when I run the class from an ASP.NET page. It works fine
if i call the class from a console application

It is not an asp.net web service that I am calling, it is an classic asp
page. It is a 3rd party site, so I have no control over it.
 
Thus wrote Darren,
I only get the error when I run the class from an ASP.NET page. It
works fine if i call the class from a console application

Hm... that seems totally bizarre to me.

Well, just to be sure, you could switch your implementation to WebClient.UploadData()
and see if that makes a difference.

Cheers,
 
Joerg

I found the problem with my code.
I needed to create a WebProxy object and add it to the HttpWebRequest object.

Darren
 
Thus wrote Darren,
Joerg

I found the problem with my code.
I needed to create a WebProxy object and add it to the HttpWebRequest
object.

Ah... yes, of course. The console app runs under your identity and thus can
obtain your system's default proxy. The ASP.NET user quite likely doesn't
have a proxy configuration.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top