Click here to Skip to main content
15,902,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the underlying connection was closed the connection was closed unexpectedly.

I getting above error wen i trying to send sms throug application please find below code

System.Object stringpost = "User=" + User + "&passwd=" + password + "&mobilenumber=" + Mobile_Number + "&message=" + Message + "&MType=" + Mtype + "&DR=" + DR + "&SID=" + SID;

            //string functionReturnValue = null;
            //functionReturnValue = "";

            HttpWebRequest objWebRequest = null;
            HttpWebResponse objWebResponse = null;
            StreamWriter objStreamWriter = null;
            StreamReader objStreamReader = null;

                string stringResult = null;

                objWebRequest = (HttpWebRequest)WebRequest.Create("");
                objWebRequest.Method = "POST";

                if ((objProxy1 != null))
                    objWebRequest.Proxy = objProxy1;

                // Use below code if you want to SETUP PROXY.
                //Parameters to pass: 1. ProxyAddress 2. Port
                //You can find both the parameters in Connection settings of your internet explorer.

                //WebProxy myProxy = new WebProxy("YOUR PROXY", PROXPORT);
                //myProxy.BypassProxyOnLocal = true;
                //wrGETURL.Proxy = myProxy;
                objWebRequest.ContentType = "application/x-www-form-urlencoded";

                objStreamWriter = new StreamWriter(objWebRequest.GetRequestStream());

                objWebResponse = (HttpWebResponse)objWebRequest.GetResponse();
                objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
                stringResult = objStreamReader.ReadToEnd();

                return (stringResult);
            catch (Exception ex)
                return (ex.Message);
Updated 21-Aug-13 17:41pm
BotCar 21-Aug-13 9:03am    
Two hints:
1. When catching an exception, return "ex.ToString()" rather than "ex.Message". ToString has the message along with some other info, such as a stack trace. This can be very useful in figuring out where exactly the problem is.
2. Place a breakpoint in Visual Studio and step through the code. See where it breaks. Inspect the values of variables to ensure that they have the values you expect them to have (you can mouseover a variable or property to see its value).

When you know more, edit your question to include the extra details (such as on what line the exception is thrown). While you're editing your question, also please place the code in a code block - that makes it much easier to read.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900