-1

I am trying to call external API with request for which I have an URL with credentials.

The external API is not accessible from browsers.

string url = "https://ICSTESTQSic-tkmdlms.integration.ocp.oraclecloud.com:443/ic/api/integration/v1/flows/rest/TSA2ELOQUA_NURTURELEADS/1.0/NurturingLead";
        using (var client = new WebClient())
        {
            NetworkCredential credentials = new NetworkCredential("Username", "Password");
            client.Credentials = credentials;
            CredentialCache cc = new CredentialCache();
            cc.Add(
                new Uri(url),
                "Basic",
                new NetworkCredential("Username", "Password"));
            client.Credentials = cc;            

            using (Stream stream = client.OpenRead(url))
            using (StreamReader reader = new StreamReader(stream))
            {
                MessageBox.Show(reader.ReadToEnd());
            }

I want to call external API by sending an object as a request from my c# code. The API is authenticated.

Lakshya
  • 17
  • 4
  • A URL makes not an API. Adding the word REST gives only a vague shape of an API, specifying the HTTP verbs that are allowed. – Aron Jan 03 '19 at 05:07

1 Answers1

0

If you use webrequest or HttpClient,you could write as follows to send an object to the server.(assume your code about authenticating is right)

WebRequest request = WebRequest.Create("http://localhost:60956/api/values");


        YClass yClass = new YClass() { Completion = 5, Message = "AA", Result = "Result" };
        JavaScriptSerializer serialize = new JavaScriptSerializer(); // the object is used to serialize the data , you could use any object that could serialize data
        byte[] objectContent = Encoding.UTF8.GetBytes(serialize.Serialize(yClass));
        request.ContentLength = objectContent.Length;
        request.ContentType = "application/json";
        request.Method = "POST";
        using (var stream = request.GetRequestStream())
        {
            stream.Write(objectContent, 0, objectContent.Length);
            stream.Close();
        }


        var resp = request.GetResponse();
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            string s = sr.ReadToEnd();
            System.Console.WriteLine(s);
        }

The result.

enter image description here

Ackelry Xu
  • 756
  • 3
  • 6
  • I used your method but I am getting error when getting the stream.Error is:- "The underlying connection was closed. An Unexpected error occured on a send." – Lakshya Jan 03 '19 at 12:43
  • You could refer to the link below, it combines posting object and sending certificate to server , here it use content-type application/x-www-form-urlencoded to send a post request, if your content-type is application-json, please use json [https://stackoverflow.com/questions/39528973/force-httpwebrequest-to-send-client-certificate](https://stackoverflow.com/questions/39528973/force-httpwebrequest-to-send-client-certificate) – Ackelry Xu Jan 04 '19 at 01:30
  • Thanks for your response. I am able to send request but getting same error "The underlying connection was closed" on this line of code "HttpWebResponse response = (HttpWebResponse)request.GetResponse()". I am using .NET 4.0. Can you help me out from this. – Lakshya Jan 08 '19 at 04:42