休息服务使用C#代码抛出内部错误500 [英] Rest service throws a internal error 500 with C# code
本文介绍了休息服务使用C#代码抛出内部错误500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用C#代码访问其他服务时遇到错误,但它适用于邮递员请求
这里是我写的代码
它在执行此代码时抛出错误
i get error while accessing rest service with C# code but it works on postman request
here is the code that i have written
it throws the error when it executes this code
HttpWebResponse response =(HttpWebResponse) request.GetResponse();
我的尝试:
What I have tried:
try
{
HttpWebRequest request =(HttpWebRequest) WebRequest.Create("https://smsmate.lk/api/sms/v2/send");
request.Method = "POST";
request.PreAuthenticate = true;
request.Headers["Authorization"] = "ApiKey" + "XXXXXX-150a-4fae-9f43-ce6e49b92d4e";
Mysms mymsg = new Mysms();
mymsg.Message = "Test";
mymsg.Addresses = "94719254547";
// JsonConvert.DeserializeObject(mymsg);
var postdata = "{" + "Message:" + mymsg.Message + "," + "Addresses:[" + mymsg.Addresses + "]}";
byte[] byteArray = Encoding.UTF8.GetBytes(postdata);
var data = Encoding.ASCII.GetBytes(postdata);
//if (ContainsUnicodeCharacter(postdata))
//{
// data = Encoding.UTF8.GetBytes(postdata);
//}
request.ContentType = "application/json";
request.ContentLength = postdata.Length;
Stream req = request.GetRequestStream();
req.Write(byteArray, 0, byteArray.Length);
req.Close();
HttpWebResponse response =(HttpWebResponse) request.GetResponse();
req = response.GetResponseStream();
}
catch (Exception ex)
{
throw ex;
}
推荐答案
听起来像一个非常脆弱的API服务器......它应该验证入站请求并给出错误回复。
使用 Fiddler(免费网络调试代理 - Telerik) [ ^ ]调试发送的实际数据并与Postman进行比较。有可能你没有发送完全相同的东西。
Sounds like a very fragile API server... It should validate inbound requests and give error responses.
Use something like Fiddler (Free Web Debugging Proxy - Telerik)[^] to debug the actual data being send and compare with Postman. Chances are that you are not sending the exact same thing.
这篇关于休息服务使用C#代码抛出内部错误500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文