【问题标题】:Problems using Mashape API in .NET在 .NET 中使用 Mashape API 的问题
【发布时间】:2013-05-02 22:48:12
【问题描述】:

我在网上找到了一些很棒的代码,用于使用 Mashape API 获取二维码。 Here.

他们的代码效果很好,但是如果我尝试将其更改为使用另一个 Mashape,我会得到一个

"运行过程中出现异常,使得结果 无效”

错误。还有一些关于检查内部异常的事情。

下面的代码显示了链接中的二维码,我尝试使用 Wea​​ther API 和 Spotify API。

private void Button_Click(object sender, RoutedEventArgs e)
{
    WebClient client = new WebClient();
    client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    client.Headers["X-Mashape-Authorization"] = "mashape-api-code-goes-here";

    StringBuilder postData = new StringBuilder();
    // For Spotify (this doesn't work)
    // postData.AppendFormat("{0}={1}", "q", HttpUtility.UrlEncode(txtArtitst.Text));
    // var uri = new Uri("https://mager-spotify-web.p.mashape.com/search/1/artist.json?", UriKind.Absolute);

    // For Weather info (this doesn't work)
    postData.AppendFormat("{0}={1}", "_method", HttpUtility.UrlEncode("getForecasts"));
    postData.AppendFormat("&{0}={1}", "location", HttpUtility.UrlEncode("brisbane"));
    var uri = new Uri("https://george-vustrey-weather.p.mashape.com/api.php?", UriKind.Absolute);

    // For QR Code (this works)
    //postData.AppendFormat("{0}={1}", "content", HttpUtility.UrlEncode("TestQRCode"));
    //postData.AppendFormat("&{0}={1}", "quality", HttpUtility.UrlEncode("L"));
    //postData.AppendFormat("&{0}={1}", "type", HttpUtility.UrlEncode("text"));
    //postData.AppendFormat("&{0}={1}", "size", HttpUtility.UrlEncode("5"));
    //var uri = new Uri("https://mutationevent-qr-code-generator.p.mashape.com/generate.php?", UriKind.Absolute);

    client.UploadStringAsync(uri, "POST", postData.ToString());
    txtResult.Text = postData.ToString();

    progress1.IsIndeterminate = true;
    button1.IsEnabled = false;
}


void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
    progress1.IsIndeterminate = false;

    var client = sender as WebClient;
    client.UploadStringCompleted -= client_UploadStringCompleted;
    string response = string.Empty;
    if (!e.Cancelled)
    {
        try
        {
            response = HttpUtility.UrlDecode(e.Result);  // seems to error out here - result invalid
            txtResult.Text = response;
        }
        catch (Exception ex)
        {
            txtResult.Text = "ERROR : " + ex.Message;
        }
    }

    button1.IsEnabled = true;
}

内部异常说:

内部异常

    {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}   System.Exception {System.Net.WebException}

【问题讨论】:

  • 内部异常是怎么说的?
  • 添加了内部异常
  • 这看起来很可疑:client.Headers["X-Mashape-Authorization"] = "mashape-api-code-goes-here";
  • 当然,我已经将我正在使用的 API 密钥替换为文本“mashape-api-code-goes-here”,这样其他人就无法使用它了。
  • 您能否提供一个示例项目,以便更快地进行调查?

标签: c# .net exception mashape


【解决方案1】:

您是否尝试过 GET 而不是 POST?

另外,你可以试试 application/json 代替 application/x-www-form-urlencoded

此处是 Windows 8 上的工作示例 - https://github.com/ismaelc/Mashape_Windows8_Sample/blob/master/App5/App5/MainPage.xaml.cs

【讨论】:

  • 这两项更改我仍然收到“TargetInvocationException”错误。我会看看你的例子并重新编码我的匹配,看看我是怎么做的。
  • 这是另一个 repo - github.com/ismaelc/MashapeFBSentimentExample 你可以忽略他们不再适用的 consumerkey 和 consumersecret 密钥
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 2014-07-26
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多