0

I want to call to a API using a class library.

Here is the way they need us to send the post request enter image description here

Or

enter image description here

To achecive this I use this code

   public class Keey
        {
            public string username { get; set; }
            public string passward { get; set; }
            public string src { get; set; }
            public string dst { get; set; }
            public string msg { get; set; }
            public string dr { get; set; }

        }

public void SendSms(string phoneNo, string SMS)
    {
        Keey account = new Keey
        {

            username = "*****",
            passward = "*********",
            src = "test.com",
            dst = phoneNo,
            msg = SMS,
            dr = "1",
           
            
        };



        WebRequest request = WebRequest.Create("http://testserver.com ");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = SMS.Length;
        CredentialCache.DefaultNetworkCredentials.UserName = account.username;
        CredentialCache.DefaultNetworkCredentials.Password = account.passward;
        request.Credentials = CredentialCache.DefaultNetworkCredentials;


        WebResponse response = request.GetResponse();

    }

But I dont know where to add those port,src,dst,msg etc... Please help me

Code is in C#

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41
  • Put port number at end of URI : "http://testserver.com:5001". A MIME attachment starts with two dashes on a new line. See : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140) – jdweng Jun 23 '21 at 07:02
  • You should pass those details as parameters. See https://stackoverflow.com/questions/3279888/how-to-add-parameters-into-a-webrequest – Gary Jun 23 '21 at 07:03

1 Answers1

0

That is depend on your Web-Api Service. for what input parameter needed.

There are several ways to perform POST requests:

HttpWebRequest (not recommended for new work)

using System.Net;
using System.Text;  // For class Encoding
using System.IO;    // For StreamReader


var request = (HttpWebRequest)WebRequest.Create("http://testserver.com/test.aspx");

//your data should place in here
var postData = "thing1=" + Uri.EscapeDataString("hello");
    postData += "&thing2=" + Uri.EscapeDataString("world");
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

recommended way to get this work:

using System.Net.Http;



private static readonly HttpClient client = new HttpClient();


POST

var values = new Dictionary<string, string>
{
    { "thing1", "hello" },
    { "thing2", "world" }
};

var content = new FormUrlEncodedContent(values);

var response = await client.PostAsync("http://testserver.com/test.aspx", content);

var responseString = await response.Content.ReadAsStringAsync();
Nima Talebi
  • 797
  • 1
  • 7
  • 21