Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:19:07+00:00 2026-05-13T09:19:07+00:00

I´m trying to use a Webservice in a .Net Compact Framework 3.5 Project which

  • 0

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: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();
        }
    }

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: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;
    }

    /// <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;
        }
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:19:07+00:00Added an answer on May 13, 2026 at 9:19 am

    You are passing the wrong value to the Content-Length header. It should be the length of the content in bytes not the count of unicode characters in the string (StreamWriter also adds the UTF8 preamble). I recommend not using the preamble and converting the string to a byte array before setting the Content-Length header value. Also, passing the username and password using the req.Credentials property might be a good idea.

    internal void SendSms()
    {
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://api.sipgate.net/RPC2");
     req.Credentials = new NetworkCredential("username", "password");
     req.Method = @"POST";
     req.ContentType = @"text/xml";
     byte[] data = Encoding.UTF8.GetBytes(Body);
     req.ContentLength = data.Length;
     using(Stream stream = req.GetRequestStream())
      stream.Write(data, 0, data.Length);
     using(Stream responseStream = req.GetResponse().GetResponseStream())
     using(StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
     {
      string result = reader.ReadToEnd();
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you can do as an alternative is to use… May 13, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer You can use ASM to parse the .class file without… May 13, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer Having the data access right there within the presentation layer… May 13, 2026 at 3:25 pm

Related Questions

I´m trying to use Interface Builder (IB) to gain time in my app development.
I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to
I´m still trying to eleminate the need of a cobol compiler in a Project
I´m trying to separate a gridViewColumn into two rows. Im using default style for
I´m trying to break a number into an array of numbers (in php) in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.