IIS asp.net C# API Post Method error "'The remote server returned an error: (403) Forbidden.'"
IIS asp.net C# API Post Method error "'The remote server returned an error: (403) Forbidden.'"
我通过 gupshup 创建了一个 viber 机器人。我 运行 我的 WebForm 应用程序在 win 10 中使用 IIS 服务器。我试图通过 api post 方法向我的 viberbot 发送消息,但 c# 扼杀了我。(我测试了 url和参数成功)
这是我的代码:
protected void viber_msg(String viberid, String strmsg)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.gupshup.io/sm/api/bot/mybotname/msg?apikey=mykey");
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "context={'botname':'mybotname','channeltype':'viber','contextid':'viberid','contexttype':'p2p'}&message="+strmsg;
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
viber_msg("viberuserID", "This is a message");
}
我得到的错误是“System.Net.WebException: 'The remote server returned an error: (403) Forbidden.'
”
还尝试使用 POSTMAN 并获得 "message": "Invalid authentication credentials"
提前谢谢...
protected void viber_msg(String viberid, String message)
{
var client = new RestClient("https://api.gupshup.io/sm/api/bot/mybot/msg?apikey=myapikey");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("context", "{\"botname\": \"mybot\",\"channeltype\" :\"viber\",\"contextid\": \""+viberid+"\",\"contexttype\": \"p2p\"}");
request.AddParameter("message", message);
IRestResponse response = client.Execute(request);
}
protected void Button1_Click(object sender, EventArgs e)
{
viber_msg("viberid", "message");
}
}
RestSharp 库!!!
我通过 gupshup 创建了一个 viber 机器人。我 运行 我的 WebForm 应用程序在 win 10 中使用 IIS 服务器。我试图通过 api post 方法向我的 viberbot 发送消息,但 c# 扼杀了我。(我测试了 url和参数成功)
这是我的代码:
protected void viber_msg(String viberid, String strmsg)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.gupshup.io/sm/api/bot/mybotname/msg?apikey=mykey");
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "context={'botname':'mybotname','channeltype':'viber','contextid':'viberid','contexttype':'p2p'}&message="+strmsg;
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
viber_msg("viberuserID", "This is a message");
}
我得到的错误是“System.Net.WebException: 'The remote server returned an error: (403) Forbidden.'
”
还尝试使用 POSTMAN 并获得 "message": "Invalid authentication credentials"
提前谢谢...
protected void viber_msg(String viberid, String message)
{
var client = new RestClient("https://api.gupshup.io/sm/api/bot/mybot/msg?apikey=myapikey");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("context", "{\"botname\": \"mybot\",\"channeltype\" :\"viber\",\"contextid\": \""+viberid+"\",\"contexttype\": \"p2p\"}");
request.AddParameter("message", message);
IRestResponse response = client.Execute(request);
}
protected void Button1_Click(object sender, EventArgs e)
{
viber_msg("viberid", "message");
}
}
RestSharp 库!!!