2013-05-02 2 views
1

Mashape API를 사용하여 QR 코드를 얻는 데 유용한 코드가 넷에서 발견되었습니다. Here..NET에서 Mashape API를 사용할 때의 문제

그들의 코드는 잘 작동하지만 시도하고 또 다른 Mashape를 사용하도록 변경하면, 나는

"An exceptions occured during the operation, making the result invalid"

오류가 발생합니다. 그리고 내부 예외를 검사하는 것에 대해서.

아래 코드는 링크의 QR 코드를 보여주고 Weather 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; 
} 

내부 예외는 말한다 :

의 InnerException

{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} 
+0

내부 예외는 무엇을 말하는가 - 또한

, 당신은 응용 프로그램/JSON 대신 x-www-form-urlencoded를 여기에 윈도우 8에

동작하는 예제 응용 프로그램을 /를 시도 할 수 있습니다? –

+0

내부 예외가 추가되었습니다. –

+0

의심스러운 것으로 보입니다. client.Headers [ "X-Mashape-Authorization"] = "mashape-api-code-go-here"; –

답변

0
+0

두 변경 사항 모두에서 여전히 "TargetInvocationException"오류가 발생합니다. 나는 당신의 모범을 보게 될 것이고, 나의 것을 재 코딩하여 내가 어떻게 움직이는 지 알게 될 것이다. –

+0

다른 repo - https://github.com/ismaelc/MashapeFBSentimentExample 더 이상 적용되지 않는 consumerkey 및 consumersecret 키는 무시할 수 있습니다. –

관련 문제