Click here to Skip to main content
14,026,083 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
i many times ask many forums including code project too how to send sms by android cell phone by using my vb.net application then i just found a application my mobilekit apk java android developed and installed in cell phone then it get a net work ip address of internet public and local ip address and port number 1688 and then i found application which is developed in c sharp there is a ip field where i input ip of cell phone and port number 1688 which is provided by cell phone and i can received and send sms by easy as i want just i do not know c# code so please help me to understand it and i will write in vb.net please

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
using System.Threading.Tasks;

using MessagingApp.Model;

namespace MessagingApp
{
    /// <summary>
    /// HTTP gateway demo form.
    /// </summary>
    public partial class MessagingApp : Form
    {
        private const string NetworkInfoUrlPath = "services/api/status/network";

        private const string BatteryInfoUrlPath = "services/api/status/battery";

        private const string MessagesUrlPath = "services/api/messaging";

        private const string MessageStatusUrlPath = "services/api/messaging/status";


        /// <summary>
        /// Initializes a new instance of the <see cref="MessagingApp"/> class.
        /// </summary>
        public MessagingApp()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the frmHttp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void frmHttp_Load(object sender, EventArgs e)
        {
            // Set focus
            this.ActiveControl = txtIPAddress;
        }

        private void btnRetrieveAllMessages_Click(object sender, EventArgs e)
        {
            RetrieveAllMessages();
        }


        private async void RetrieveAllMessages()
        {

            if (string.IsNullOrEmpty(txtIPAddress.Text))
            {
                MessageBox.Show("Please enter an IP address", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtIPAddress.Focus();
                return;
            }


            try
            {

                btnRetrieveAllMessages.Enabled = false;

                /*
                HttpClientHandler handler = new HttpClientHandler();
                if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
                {
                    handler.UseDefaultCredentials = true;
                    handler.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text);
                }
                */

                using (var client = new HttpClient())
                {

                    string url = ConstructBaseUri();
                    client.BaseAddress = new Uri(url);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                                    "Basic",
                                     Convert.ToBase64String(
                                     ASCIIEncoding.ASCII.GetBytes(
                                     string.Format("{0}:{1}", txtUserName.Text, txtPassword.Text))));
                    }


                    HttpResponseMessage response = await client.GetAsync(MessagesUrlPath);
                    if (response.IsSuccessStatusCode)
                    {
                        GetMessageResponse result = await response.Content.ReadAsAsync<GetMessageResponse>();
                        if (result.IsSuccessful)
                        {
                            txtOutput.Clear();
                            foreach (DeviceMessage msg in result.Messages)
                            {
                                AddToOutput(msg.ToString());
                                AddToOutput("");
                            }
                        }
                        else
                        {
                            MessageBox.Show(result.Description, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show(response.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                btnRetrieveAllMessages.Enabled = true;
            }
        }


        /// <summary>
        /// Builds the URL.
        /// </summary>
        /// <returns></returns>
        protected string ConstructBaseUri()
        {
            UriBuilder uriBuilder = new UriBuilder("http", txtIPAddress.Text, Convert.ToInt32(txtPort.Text));
            return uriBuilder.ToString();
        }

        private void AddToOutput(string text)
        {
            txtOutput.AppendText(text + "\r\n");
        }

        private void btnRetrieveNewMessages_Click(object sender, EventArgs e)
        {
            RetrieveNewMessages();
        }

        private async void RetrieveNewMessages()
        {
            if (string.IsNullOrEmpty(txtIPAddress.Text))
            {
                MessageBox.Show("Please enter an IP address", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtIPAddress.Focus();
                return;
            }

            try
            {

                btnRetrieveNewMessages.Enabled = false;

                using (var client = new HttpClient())
                {

                    string url = ConstructBaseUri();
                    client.BaseAddress = new Uri(url);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                                    "Basic",
                                     Convert.ToBase64String(
                                     ASCIIEncoding.ASCII.GetBytes(
                                     string.Format("{0}:{1}", txtUserName.Text, txtPassword.Text))));
                    }


                    HttpResponseMessage response = await client.GetAsync(MessagesUrlPath + "?status=0");
                    if (response.IsSuccessStatusCode)
                    {
                        GetMessageResponse result = await response.Content.ReadAsAsync<GetMessageResponse>();
                        if (result.IsSuccessful)
                        {
                            txtOutput.Clear();
                            foreach (DeviceMessage msg in result.Messages)
                            {
                                AddToOutput(msg.ToString());
                                AddToOutput("");
                            }
                        }
                        else
                        {
                            MessageBox.Show(result.Description, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show(response.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                btnRetrieveNewMessages.Enabled = true;
            }
        }

        private void btnSendMessage_Click(object sender, EventArgs e)
        {
            SendMessage();
        }

        private async void SendMessage()
        {

            if (string.IsNullOrEmpty(txtIPAddress.Text))
            {
                MessageBox.Show("Please enter an IP address", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtIPAddress.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtContact.Text) || string.IsNullOrEmpty(txtMessage.Text))
            {
                MessageBox.Show("Please enter the contact name/number and the message", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtContact.Focus();
                return;
            }

            try
            {

                btnSendMessage.Enabled = false;

                using (var client = new HttpClient())
                {

                    string url = ConstructBaseUri();
                    client.BaseAddress = new Uri(url);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                                    "Basic",
                                     Convert.ToBase64String(
                                     ASCIIEncoding.ASCII.GetBytes(
                                     string.Format("{0}:{1}", txtUserName.Text, txtPassword.Text))));
                    }


                    var postData = new List<KeyValuePair<string, string>>();
                    postData.Add(new KeyValuePair<string, string>("to", txtContact.Text));
                    postData.Add(new KeyValuePair<string, string>("message", txtMessage.Text));
                    HttpContent content = new FormUrlEncodedContent(postData); 

                    HttpResponseMessage response = await client.PostAsync(MessagesUrlPath, content);
                    if (response.IsSuccessStatusCode)
                    {
                        PostMessageResponse result = await response.Content.ReadAsAsync<PostMessageResponse>();
                        if (result.IsSuccessful)
                        {
                            txtOutput.Clear();
                            AddToOutput(result.ToString());
                            AddToOutput("");
                        }
                        else
                        {
                            MessageBox.Show(result.Description, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show(response.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                btnSendMessage.Enabled = true;
            }
        }
    }
}
Posted
Updated 18-Mar-19 5:01am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It's a "form" written in C#.

You can call C# forms from a VB.Net app; you don't have to "rewrite in VB.Net".

Use VB.NET and C# in the same application? - Stack Overflow[^]

How to call C# form from a VB.NET form????-VBForums[^]
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

To add to what Gerry says, try this as well: Code Converter C# to VB and VB to C# – Telerik[^] - it may help you understand it for yourself.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190419.4 | Last Updated 18 Mar 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100