2013-05-17 14 views
16

Ho letto alcuni altri post su Stack ma non riesco a farlo funzionare. Funziona benissimo sulla mia quando ho eseguito il comando curl in git sulla mia macchina Windows, ma quando mi converto ad ASP.NET non funziona:curl Richiesta con ASP.NET

private void BeeBoleRequest() 
    { 
     string url = "https://mycompany.beebole-apps.com/api"; 

     WebRequest myReq = WebRequest.Create(url);    

     string username = "e26f3a722f46996d77dd78c5dbe82f15298a6385"; 
     string password = "x"; 
     string usernamePassword = username + ":" + password; 
     CredentialCache mycache = new CredentialCache(); 
     mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password)); 
     myReq.Credentials = mycache; 
     myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword))); 

     WebResponse wr = myReq.GetResponse(); 
     Stream receiveStream = wr.GetResponseStream(); 
     StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8); 
     string content = reader.ReadToEnd(); 
     Response.Write(content); 
    } 

Questa è l'API BeeBole. E 'piuttosto semplice. http://beebole.com/api ma sto ottenendo il seguente errore 500 quando eseguo quanto sopra:

Il server remoto ha restituito un errore: (500) Errore interno del server.

risposta

21

Il metodo HTTP predefinito per WebRequest è GET. Prova a impostarlo su POST, poiché è ciò che l'API si aspetta

myReq.Method = "POST"; 

Suppongo che stiate postando qualcosa. Come test, pubblicherò gli stessi dati dal loro esempio di arricciatura.

string url = "https://YOUR_COMPANY_HERE.beebole-apps.com/api"; 
string data = "{\"service\":\"absence.list\", \"company_id\":3}"; 

WebRequest myReq = WebRequest.Create(url); 
myReq.Method = "POST"; 
myReq.ContentLength = data.Length; 
myReq.ContentType = "application/json; charset=UTF-8"; 

string usernamePassword = "YOUR API TOKEN HERE" + ":" + "x"; 

UTF8Encoding enc = new UTF8Encoding(); 

myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(enc.GetBytes(usernamePassword))); 


using (Stream ds = myReq.GetRequestStream()) 
{ 
ds.Write(enc.GetBytes(data), 0, data.Length); 
} 


WebResponse wr = myReq.GetResponse(); 
Stream receiveStream = wr.GetResponseStream(); 
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8); 
string content = reader.ReadToEnd(); 
Response.Write(content); 
+1

Grazie. Ha funzionato a meraviglia – Dkong

Problemi correlati