Note that there are some explanatory texts on larger screens.

plurals
  1. PO.Net CF / HttpWebRequest and a "WebException was unhandled"
    primarykey
    data
    text
    <p>I´m trying to use a Webservice in a .Net Compact Framework 3.5 Project which has no WSDL and where I have to use HttpWebRequest. I´ve tried my code on 2 Devices and on the Emulator but I get everytime the same Exception and I really don´t get why!?</p> <p>First, my code:</p> <pre><code>internal void SendSms() { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://username:password@api.sipgate.net/RPC2"); req.Method = @"POST"; req.ContentType = @"text/xml"; req.ContentLength = Body.Length; using (Stream stream = req.GetRequestStream()) using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { writer.Write(Body); } using (Stream responseStream = req.GetResponse().GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { string result = reader.ReadToEnd(); } } </code></pre> <p>In line "using (Stream stream = req.GetRequestStream())" I get the following exception and I can´t figure out why:</p> <p>System.Net.WebException {"Could not establish connection to network."}</p> <p>Stacktrace:</p> <p>at System.Net.HttpWebRequest.finishGetRequestStream() at System.Net.HttpWebRequest.GetRequestStream() at SipMSGate.UI.MainFormController.SendSms() at SipMSGate.UI.Form1.menuItem1_Click(Object sender, EventArgs e) at System.Windows.Forms.MenuItem.OnClick(EventArgs e) at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam) at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam) at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) at System.Windows.Forms.Application.Run(Form fm) at SipMSGate.Program.Main()</p> <p>Status:</p> <p>System.Net.WebExceptionStatus.ConnectFailure</p> <p>I can use the Internet explorer on the Devices and on the Emulator, so I think that I have an internet Connection.</p> <p>Any Idea what´s wrong or what I forget in my code?</p> <p>Than you so much</p> <p>twickl</p> <p>Here is now the complete Code including Yakimych's Code that gives the xception on 2 Devices and the Emulator Images which all of them are having a connection to the Internet:</p> <pre><code>using System.Drawing; using System.IO; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Windows.Forms; namespace httpreqTest { public partial class Form1 : Form { private HttpWebRequest _req; private bool _ignoreCertificateErrors; private string _errorMessage; private const string Body = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;methodCall&gt;&lt;methodName&gt;samurai.SessionInitiate&lt;/methodName&gt;&lt;params&gt;&lt;param&gt;&lt;value&gt;&lt;struct&gt;&lt;member&gt;&lt;name&gt;LocalUri&lt;/name&gt;&lt;value&gt;&lt;string&gt;&lt;/string&gt;&lt;/value&gt;&lt;/member&gt;&lt;member&gt;&lt;name&gt;RemoteUri&lt;/name&gt;&lt;value&gt;&lt;string&gt;01234556789&lt;/string&gt;&lt;/value&gt;&lt;/member&gt;&lt;member&gt;&lt;name&gt;TOS&lt;/name&gt;&lt;value&gt;&lt;string&gt;text&lt;/string&gt;&lt;/value&gt;&lt;/member&gt;&lt;member&gt;&lt;name&gt;Content&lt;/name&gt;&lt;value&gt;&lt;string&gt;This is a Test&lt;/string&gt;&lt;/value&gt;&lt;/member&gt;&lt;member&gt;&lt;name&gt;Schedule&lt;/name&gt;&lt;value&gt;&lt;string&gt;&lt;/string&gt;&lt;/value&gt;&lt;/member&gt;&lt;/struct&gt;&lt;/value&gt;&lt;/param&gt;&lt;/params&gt;&lt;/methodCall&gt;"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this._ignoreCertificateErrors = true; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byte1 = encoding.GetBytes(Body); CreateWebRequestObject(@"https://user:pass@api.sipgate.net/RPC2"); _req.Method = @"POST"; _req.ContentType = @"text/xml"; _req.ContentLength = byte1.Length; using (Stream stream = _req.GetRequestStream()) using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { writer.Write(Body); } using (Stream responseStream = _req.GetResponse().GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { string result = reader.ReadToEnd(); } } public bool CreateWebRequestObject(string Url) { try { this._req = (HttpWebRequest)System.Net.WebRequest.Create(Url); if (this._ignoreCertificateErrors) ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy(); } catch (Exception ex) { this._errorMessage = ex.Message; return false; } return true; } /// &lt;summary&gt; /// Internal object used to allow setting WebRequest.CertificatePolicy to /// not fail on Cert errors /// &lt;/summary&gt; internal class AcceptAllCertificatePolicy : ICertificatePolicy { public AcceptAllCertificatePolicy() { } public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb) { // *** Always accept return true; } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload