Israel

Israel

  • 701
  • 1.3k
  • 217.3k

The server remote return an erro: (401) Not authorized.

Jul 9 2021 8:41 AM

Hi,

I am trying to send SMS. But my program running dispaying a error message:

System.Net.WebException: 'The server remote return an erro: (401) Not authorized.'

On the line 51: HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String url = "https://xxxxxxxxxxxxxxxx";                                               //The SMS HTTP API send url.
            HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);   //Create object of class required for POST request.
                                                                                //Initialize post object.
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "[email protected]";                          //The HTTP API username of your account.
            postData += "&xxxxx";                                                                 //The HTTP API password of your account.
            postData += "&msgtext=This is my test";                                         //The SMS Message text.
            postData += "&originator=Hi friend";                                             //The desired Originator of your message.
            postData += "&+243855291139";                                                  //The full International mobile number of the
                                                                                           //recipient excluding the leeding +.
            postData += "&showDLR=0";                                                       //Set to 1 for requesting delivery report
                                                                                            //of this sms. A unique id is returned to use
                                                                                            //with your delivery report request.
            postData += "&charset=0";                                                     //The SMS Message text Charset.
            postData += "&msgtype=";                                                   //If set to F the sms is sent as Flash.
            postData += "&smsprovider=solutions4mobiles.com"; //The SMS Provider.
            byte[] data = encoding.GetBytes(postData);
            httpWReq.Method = "POST";
            httpWReq.ContentType = "application/x-www-form-urlencoded";
            httpWReq.ContentLength = data.Length;

            //Write parameters to POST request and send it.

            using (Stream stream = httpWReq.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();//THE BUGG IT'S HERE

            //Get Response
            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            System.Diagnostics.Debug.Print(responseString);
        }
    }

 


Answers (2)