user3179063
user3179063

Reputation: 21

How to use WebRequest in c#

I'am trying to use example API call in below link; please check link:

http://sendloop.com/help/article/api-001/getting-started

My account is "code5" so I tried 2 codes to get systemDate.

  1. Code

     var request = WebRequest.Create("http://code5.sendloop.com/api/v3/System.SystemDate.Get/json");
     request.ContentType = "application/json; charset=utf-8";
    
     string text;
     var response = (HttpWebResponse)request.GetResponse();
    
     using (var sr = new StreamReader(response.GetResponseStream()))
     {
         text = sr.ReadToEnd();
     }
    
  2. Code

     HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://code5.sendloop.com/api/v3/System.SystemDate.Get/json");
     httpWebRequest.Method = WebRequestMethods.Http.Get;
     httpWebRequest.Accept = "application/json";
    

But I don't know that I use correctly API by above codes ?

When I use above codes I don't see any data or anything.

How can I get and post API to Sendloop, and how can I use API by using WebRequest?

I will use API first time in .net so

any help will be appreciated.

Thanks.

Upvotes: 2

Views: 52193

Answers (2)

Abhishek Kanrar
Abhishek Kanrar

Reputation: 496

    string userAuthenticationURI = 
    "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+ originZip + 
    "&destinations="+ DestinationZip + "&units=imperial&language=en- 
     EN&sensor=false&key=Your API Key";
            if (!string.IsNullOrEmpty(userAuthenticationURI))
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(userAuthenticationURI);
                request.Method = "GET";
                request.ContentType = "application/json";
                WebResponse response = request.GetResponse();
                var responseString = new 
StreamReader(response.GetResponseStream()).ReadToEnd();
                dynamic obj = JsonConvert.DeserializeObject(responseString);

            }

Upvotes: 1

Cameron Tinker
Cameron Tinker

Reputation: 9789

It looks like you need to post your API key to the endpoint when making requests. Otherwise, you will not be authenticated and it will return an empty response.

To send a POST request, you will need to do something like this:

var request = WebRequest.Create("http://code5.sendloop.com/api/v3/System.SystemDate.Get/json");
request.ContentType = "application/json; charset=utf-8";

string postData = "APIKey=xxxx-xxxxx-xxxxx-xxxxx-xxxxx";

request.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream(); //open connection
newStream.Write(data, 0, data.Length); // Send the data.
newStream.Close();

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

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Upvotes: 5

Related Questions