WebException was caught. The remote server returned an error: (403) Forbidden

442 Views Asked by At

I am trying to send push notifications to iOS devices from back-end. When I run my service this exception is being thrown. "The remote server returned an error: (403) Forbidden." This is my code in c#:

string appid = "my_app_id";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.everlive.com/v1/"+appid+"/Push/Notifications");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
IWebProxy myProxy = httpWebRequest.Proxy;
myProxy.Credentials = new NetworkCredential("", "", "IFMUK");
if (myProxy != null)
{
    // Use the default credentials of the logged on user.
    myProxy.Credentials = CredentialCache.DefaultCredentials;
}
httpWebRequest.Proxy = myProxy;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string notificationMessage = "These are not the droids you are looking for";
    string json = "{\"Filter\":\"{\\\"$and\\\":[{\\\"Parameters.User\\\":\\\"" + "Luke".ToLower() + "\\\"}]}\",\"NotificationDate\":null,\"ExpirationDate\":null,\"IOS\":null,\"Android\":null,\"WindowsPhone\":null,\"Windows\":null,\"Message\":\"" + notificationMessage + "\",\"UseLocalTime\":false}";
    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
} 
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
WebHeaderCollection header = httpResponse.Headers;
var encoding = ASCIIEncoding.ASCII;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), encoding))
{
    var res = streamReader.ReadToEnd();
}

I would be grateful if someone could help me in resolving the issue. Thanks in advance!

0

There are 0 best solutions below