2015-01-02 1 views
-1

이 코드를 사용하는 동안 오류가 발생했습니다. 나는 Urban 비행선에 대한 적절한 자격 증명을 준다.REST API를 사용하여 푸시 알림을 구현하는 동안이 오류가 발생했습니다 :: 원격 서버에서 오류를 반환했습니다 : (401) Unconthorized

  WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/"); 
      request.Credentials = new NetworkCredential(UrbanAirShip UserLogin, MasterKey); 

      // Set the Method property of the request to POST. 
      request.Method = "POST"; 

      // Create POST data and convert it to a byte array. 
      //WRITE JSON DATA TO VARIABLE D 
      string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"]}"; 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

      // Set the ContentType property of the WebRequest. 
      request.ContentType = "application/json"; 

      // Set the ContentLength property of the WebRequest. 
      request.ContentLength = byteArray.Length; 

      // Get the request stream. 
      using (Stream dataStream = request.GetRequestStream()) 
      { 
       // Write the data to the request stream. 
       dataStream.Write(byteArray, 0, byteArray.Length); 
      } 

      // Get the response. 
      WebResponse response = request.GetResponse(); 

      //Error "The remote server returned an error: (400) Bad Request" 
      // Display the status. 
      Console.WriteLine(((HttpWebResponse)response).StatusDescription); 

      // Get the stream containing content returned by the server. 
      using (Stream dataStream = response.GetResponseStream()) 
      { 
       // Open the stream using a StreamReader for easy access. 
       using (var reader = new StreamReader(dataStream)) 
       { 
        // Read the content. 
        string responseFromServer = reader.ReadToEnd(); 

        // Display the content. 
        Console.WriteLine(responseFromServer); 

        response.Close(); 

        return true; 
       } 
      } 

답변

-1
 // Create a request using a URL that can receive a post. 
     WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/"); 
     // Set the Method property of the request to POST. 
     request.Method = "POST";`` 

     // Create POST data and convert it to a byte array. 
     string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"},     \"device_tokens\": [\"Device_Token\"]}"; 
     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/json"; 
     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 

     //Do a http basic authentication somehow 
     string username = "xxxx"; //App Key From Urban Airship 
     string password = "xxxxx"; //Master Secret Key From Urban Airship 
     string usernamePassword = username + ":" + password; 
     CredentialCache mycache = new CredentialCache(); 
     mycache.Add(new Uri("https://go.urbanairship.com/api/push/"), "Basic", new NetworkCredential(username, password)); 
     request.Credentials = mycache; 
     request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword))); 

     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 
관련 문제