Home php c# sql c c++ javascript python java go android git linux asp.net django .net node.js ios xcode cocoa iphone mysql tomcat mongodb bash objective-c scala visual-studio apache elasticsearch jar eclipse jquery ruby-on-rails ruby rubygems android-studio spring lua sqlite emacs ubuntu perl docker swift amazon-web-services svn html ajax xml java-ee maven intellij-idea rvm macos unix css ipad postgresql css3 json windows-server vue.js typescript oracle hibernate internet-explorer github tensorflow laravel symfony redis html5 google-app-engine nginx firefox sqlalchemy lucene erlang flask vim solr webview facebook zend-framework virtualenv nosql ide twitter safari flutter bundle phonegap centos Register | Login | Edit Tags | New Questions | 繁体 | 简体

0
votes
answers
楼主
0 views
+10

將cURL代碼轉換爲c#izooto api

1

我正在嘗試訪問api以進行推送通知。將cURL代碼轉換爲c#izooto api

這是捲曲代碼:

curl -X POST  
    -H "Authentication-Token: {API_TOKEN}"  
    -H "Content-Type: application/json"  
    -d '{ 
    "title" : "{NOTIFICATION_TITLE}", 
    "message" : "{NOTIFICATION_MESSAGE}", 
    "icon_url" : "{ICON_URL}", 
    "banner_url" : "{BANNER_URL}", 
    "landing_url" : "{LANDING_URL}", 
    "actions" : [ 
     { 
     "text" : "{BUTTON1_TEXT}", 
     "url" : "{BUTTON1_URL}" 
     }, 
     { 
     "text" : "{BUTTON2_TEXT}", 
     "url" : "{BUTTON2_URL}" 
     }], 
    "utm_source" : "{UTM_SOURCE}", 
    "utm_medium" : "{UTM_MEDIUM}", 
    "utm_campaign" : "{UTM_CAMPAIGN}", 
    "ttl" : {TTL_SECONDS}, 
    "target" : { 
      "type" : "all" 
    } 
    }' "https://apis.izooto.com/v1/notifications" 

我試圖通過C#來訪問API。

C#代碼:

public string get() 
    { 
     try 
     { 
      WebRequest tRequest; 
      tRequest = WebRequest.Create("https://apis.izooto.com/v1/notifications"); 
      tRequest.Method = "post"; 
      tRequest.ContentType = "multipart/form-data"; 
      tRequest.Headers.Add("Authentication-Token", "xxxxxxxxx-yyyyyyyyy"); 

      string imgurl = "https://cdnimg.izooto.com/9338/9883/93381513921358.png"; 
      string landing_url = "http://www.maalaimalar.com/News/TopNews/2017/12/27110835/1136906/MK-Stalin-Slams-his-Brother-MK-Stalin-for-RK-Nagar.vpf"; 
      string postData = "title=test&message=testmsg&icon_url=" + imgurl + "&landing_url=" + landing_url + ""; 

      Console.WriteLine(postData); 

      Byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      tRequest.ContentLength = byteArray.Length; 
      Stream dataStream = tRequest.GetRequestStream(); 

      dataStream.Write(byteArray, 0, byteArray.Length); 
      dataStream.Close(); 
      WebResponse tResponse = tRequest.GetResponse(); 

      dataStream = tResponse.GetResponseStream(); 
      StreamReader tReader = new StreamReader(dataStream); 
      String sResponseFromServer = tReader.ReadToEnd(); 

      //lblStat.Text = sResponseFromServer; 
      tReader.Close(); 
      dataStream.Close(); 
      tResponse.Close(); 
      return sResponseFromServer; 
     } 
     catch (Exception e) 
     { 
      return e.Message; 
     } 
    } 

到達時,我收到以下錯誤WebResponse tResponse = tRequest.GetResponse();

錯誤:System.Net.WebException: The remote server returned an error: (400) Bad Request.

響應:{"success":false,"message":"Authentication token missing"}

我提到以下文檔卷曲碼 https://docs.izooto.com/docs/push-to-all 任何人都可以爲此提供解決方案。提前致謝。

+0

看起來對我來說,它希望JSON,但你要發送表單數據。 – Crowcoder

+0

我也嘗試過使用「application/json」 –

+0

你不能只改變內容類型頭,你還必須給POST Body有效的JSON。 – Crowcoder

沙发
0
2

通行證目標在下面postData.Like,

string postData= "{
 "title" : ""+ title + "",
 "message" : ""+ message + "",
 "icon_url" : ""+ icon_url + "",
 "banner_url" : "" + bannerUrl + "",
 "landing_url" : "" + landing_url + "",
 "target" : {
   "type" : "all"
 }
 }"; 
discard