Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2006

Tip: Looking for answers? Try searching our database.

Problem HttpWebRequest from asp.net app

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darren - 27 Mar 2006 17:06 GMT
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;
}
Joerg Jooss - 27 Mar 2006 21:49 GMT
Thus wrote Darren,

> I created a class to submit an xml document to a webpage.
>
[quoted text clipped - 11 lines]
> 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,
Signature

Joerg Jooss
news-reply@joergjooss.de

Darren - 28 Mar 2006 09:11 GMT
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.

> 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,
Joerg Jooss - 28 Mar 2006 21:39 GMT
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,
Signature

Joerg Jooss
news-reply@joergjooss.de

Darren - 29 Mar 2006 14:21 GMT
I'll create a test app and get back with my results

> Thus wrote Darren,
>
[quoted text clipped - 7 lines]
>
> Cheers,
Darren - 31 Mar 2006 16:42 GMT
Joerg

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

Darren
Joerg Jooss - 31 Mar 2006 19:55 GMT
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,
Signature

Joerg Jooss
news-reply@joergjooss.de


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.