Hello,
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!?
First, my code:
internal void SendSms()
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://username:[email protected]/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();
}
}
In line "using (Stream stream = req.GetRequestStream())" I get the following exception and I can´t figure out why:
System.Net.WebException {"Could not establish connection to network."}
Stacktrace:
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()
Status:
System.Net.WebExceptionStatus.ConnectFailure
I can use the Internet explorer on the Devices and on the Emulator, so I think that I have an internet Connection.
Any Idea what´s wrong or what I forget in my code?
Than you so much
twickl
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:
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>samurai.SessionInitiate</methodName><params><param><value><struct><member><name>LocalUri</name><value><string></string></value></member><member><name>RemoteUri</name><value><string>01234556789</string></value></member><member><name>TOS</name><value><string>text</string></value></member><member><name>Content</name><value><string>This is a Test</string></value></member><member><name>Schedule</name><value><string></string></value></member></struct></value></param></params></methodCall>";
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:[email protected]/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;
}
/// <summary>
/// Internal object used to allow setting WebRequest.CertificatePolicy to
/// not fail on Cert errors
/// </summary>
internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
}
public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb)
{
// *** Always accept
return true;
}
}
}
}