Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebRequest causing a SocketException

Status
Not open for further replies.

iaresean

Programmer
Mar 24, 2003
570
ZA
Hello all;

I have the following code to fetch an html template (via a webrequest) from an aspx page I have and then print the results of the fetch onto the calling page.

Code:
// First get the newsletter template
string sImageUrl = "";

NewsTemp oNewsTemp = NewsTemp.Get(int.Parse(Request["tem_Id"].ToString()));

string sUrl = General.GetBaseUrl(Request.ServerVariables["SERVER_NAME"].ToString()) + "templates/newsletters/" + oNewsTemp._sFilename;
sUrl += "?type=preview";

if (Request["img_Id"].ToString().Length > 0)
{
    sUrl += "&img_Id=" + Request["img_Id"].ToString();
}

HttpWebRequest  req   = (HttpWebRequest)WebRequest.Create(sUrl);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Stream st            = resp.GetResponseStream();
StreamReader sr      = new StreamReader(st);

string buffer        = sr.ReadToEnd();

sr.Close();
st.Close();

string sJavascript = "\n\n<script type=\"text/javascript\"> \n" +
    "var obj = window.opener; \n" +
    "var title = obj.document.getElementById('ctl00_MainContent_txtTitle').value; \n" +
    "var imgtext = obj.document.getElementById('ctl00_MainContent_txtImageText').value; \n" +
    "var body = obj.document.getElementById('fckBody').value; \n" +
    "document.getElementById('spnTitle').innerHTML = title; \n" +
    "document.getElementById('spnImageText').innerHTML = imgtext.toUpperCase(); \n" +
    "document.getElementById('spnText').innerHTML = body; \n" +
    "</script>";

buffer += sJavascript;

Page.Response.Write(buffer);

It runs fine on my local and test servers, however, as soon as I put it live it gives me the following error:

No connection could be made because the target machine actively refused it
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it

Source error:
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Does anybody know what could be causing this error?

I am stumped. :-(

Thank you for any and all help/advice/guidance.

Sean. [peace]
 
I imagine that this is a permissions issue and is probably something to do with how the account that is running the ASP.NET process is authenticated (i.e. Windows, anonymous etc). I'd have a play around with the permissions and make sure that they all look to be set up correctly.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
What kind of authentication does the site require? You may need to use some credentials for the request:

Code:
HttpWebRequest  req   = (HttpWebRequest)WebRequest.Create(sUrl);
[b]
//TODO: make sure your environement uses the proper credentials
req.Credentials = CredentialCache.DefaultCredentials;[/b]
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Hi BoulderBum and Ca8msm;

Thank you for your responses.

I inserted the line of code like you instructed BB, but I still recieved the same error. Does this mean that there is a permissions issue lurking here, as you say ca8msm?

Could it possibly anything else given your guys experience?

I just want to be 100% sure before I point fingers at the web host and ask them to sort things out. :)

Thanks for your help guys.



Sean. [peace]
 
Also;

I don't think the web guys are that technically minded, unfortunately.

Could you guys possibly tell me how to sort out the permissions, just in case they don't have a clue (like me). :)

Thanks again.

Sean. [peace]
 
The line of code I inserted would only work under certain configurations if authentication was the problem (it may require more code, depending), though that may not even be the issue.

If you put a breakpoint at the line where you instantiate the HttpWebRequest, what is the URL stored in the string? If you cut and paste the URL into a browser does it work fine?

Also, does the raised exception have anything more detailed in the InnerException or GetBaseException()?

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Hi Boulderbum;

Thanks for the response.

I grabbed the resulting URL and pasted it as you suggested. This worked fine, no errors.

Here is the output from the InnerException:
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

Does this make any sense to you?

Thanks for your help.

Sean. [peace]
 
If the URL resulted in a page as expected, then there's something going on with security between your app and the target site. This could be a firewall issue, something to do with authentication (are CredentialCache.DefaultCredentials null in your environment?) or something else.

What is the message of the high-level exception (check Message vs. InnerException). Is there a deeper exception than the InnerException?

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Hey Guys;

Managed to get it sorted with the web host.

It looks like the external IP was not accessible from inside their own network. The host made some changes to ensure that the domain can be resolved locally now.

All is working fine now. :)

Thanks for your help though guys.

Sean. [peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top