使用下面的代码,请求会以Get而非Post发送,我已将重定向设置为false而没有运气,请协助。
我已更新问题并包含更多代码 字符串
myURI = "https://api.bulksms.com/v1/messages";
string myUsername = "******";
string myPassword = "******";
string myData = "{to: \"7777777777\", body:\"Hello Mr. Mbhele!\"}";
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(myURI);
request.AllowAutoRedirect = false;
request.MaximumAutomaticRedirections = 0;
request.Credentials = new NetworkCredential(myUsername, myPassword);
request.PreAuthenticate = true;
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Content-Type", "appication/json");
var encoding = new UnicodeEncoding();
var encodedData = encoding.GetBytes(myData);
var stream = request.GetRequestStream();
stream.Write(encodedData, 0, encodedData.Length);
stream.Close();
try
{
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
return reader;
}
catch (WebException ex)
{
Console.WriteLine("An error occurred:" + ex.Message);
var reader = new StreamReader(ex.Response.GetResponseStream());
return reader;
}
答案 0 :(得分:0)
对于将来有类似问题的任何人。我在另一个内部调用REST API,后期请求被重定向,将第一个调用更改为[Get]而不是[Post]似乎已经完成了这个技巧。