如何使用WebRequest类在C# [英] How to use WebRequest in c#

查看:134
本文介绍了如何使用WebRequest类在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'尝试使用例如API调用在下面的链接请检查链接

I'am trying to use example api call in below link please check link

http://sendloop.com/help/article/api-001/工具入门

我的账户是code5所以我尝试2 codeS得到systemDate。

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

但我不知道我正确的API使用由上面codeS?

But i don't know that i use correctly api by above codes ?

在我上面codeS我没有看到任何数据或任何使用。

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

我怎样才能和岗位API来Sendloop.And我如何才能通过使用WebRequest的使用API​​?

How can i get and post api to Sendloop.And how can i use api by using WebRequest ?

我将使用.NET API第一次如此

I will use api first time in .net so

任何帮助将AP preciated。

感谢。

推荐答案

它看起来像你需要做的请求时,发表您的API密钥到端点。否则,你将不会被验证,它会返回一个空的响应。

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.

要发送POST请求,你需要做这样的事情:

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

这篇关于如何使用WebRequest类在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆