How to use WebRequest in c#

陌路散爱 提交于 2019-12-12 01:12:53

问题


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.


回答1:


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();
}



回答2:


    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);

            }


来源:https://stackoverflow.com/questions/21029410/how-to-use-webrequest-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!