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;
}
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;
}