gpt4 book ai didi

c# - 如何在 C# 中使用 WebRequest

转载 作者:行者123 更新时间:2023-12-02 17:40:49 28 4
gpt4 key购买 nike

我正在尝试在下面的链接中使用示例 api 调用请检查链接

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

我的帐户是“code5”,所以我尝试了 2 个代码来获取 systemDate。

<强>1。代码

        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.代码

        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?

当我使用上面的代码时,我看不到任何数据或任何东西。

我如何获取 api 并将其发布到 Sendloop。我如何通过 WebRequest 使用 api?

我会第一次在 .net 中使用 api,所以

我们将不胜感激。

谢谢。

最佳答案

看起来您需要在发出请求时将您的 API key 发布到端点。否则,您将无法通过身份验证,并且会返回空响​​应。

要发送 POST 请求,您需要执行以下操作:

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

关于c# - 如何在 C# 中使用 WebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21029410/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com