【问题标题】:How to use WebRequest in c#如何在 C# 中使用 WebRequest
【发布时间】:2014-01-09 19:30:40
【问题描述】:

我正在尝试在下面的链接中使用示例 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 所以

我们将不胜感激。

谢谢。

【问题讨论】:

  • 此端点是否接受 GET 或 POST 或两者都接受?
  • 我想我需要获取该链接中的 api 示例。但我将来也需要发布。所以如果你帮助我,我会很高兴先生。

标签: c# .net api webrequest


【解决方案1】:

您似乎需要在发出请求时将 API 密钥发布到端点。否则,您将无法通过身份验证并返回空响应。

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

【讨论】:

  • 文本显示 "{\"Success\":false,\"ErrorCode\":104,\"ErrorMessage\":\"Invalid API key\",\"ErrorFields\":[] }" 字符串
  • @user3179063 您收到该错误,因为您尚未添加 API 密钥。您需要将上面看到的所有“x”替换为您的实际 API 密钥。在此之前,API 将继续拒绝您的请求。
  • 先生,如果您有Skype,请加我“SonerSevinc92”,我会给您发送api密钥,因为它仍然给出相同的异常:(
  • @user3179063 对不起,我不是 Skype。您需要将“x”替换为您的实际 API 密钥。如果您没有 API 密钥,则需要申请一个。
  • 示例 api 调用: curl -d APIKey=xxxx-xxxxx-xxxxx-xxxxx-xxxxx \ youraccount.sendloop.com/api/v3/System.SystemDate.Get/json 那么我该如何使用“curl - d”?
【解决方案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);

            }

【讨论】:

  • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 2016-12-28
  • 2010-10-13
  • 2011-05-08
  • 1970-01-01
相关资源
最近更新 更多