不支持的媒体类型 415 c#(发送短信)

Unsupported media type 415 c# (Sending SMS)

亲爱的,

美好的一天...

当我尝试通过 "SMS API" 发送短信时,我遇到了以下异常 "Unsupported media type 415"。

我从服务提供商那里得到了以下 xml 格式

System.Net.WebRequest           req = null;
System.Net.WebResponse          rsp = null;
System.IO.StreamWriter          writer;
System.IO.StreamReader          Reader;
String                          responseFromServer;
String                          uri;
String                          txtXMLData;
String                          AccountId;
String                          Password;
String                          SecureHashSecretKey;
String                          SenderName;
String                          MSISDN;
String                          SMSMessage;
try
{
AccountId               =   "xxxxx";
Password                =   "xxxxx";
SecureHashSecretKey     =   "xxxxx";
SenderName              =   "xxxxx";
MSISDN                  =   "xxxxx";
SMSMessage              =   "Test SMS";
uri                     =   "https://e3len.vodafone.com.eg/web2sms/sms/submit/";
                    txtXMLData              =   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                                "<SubmitSMSRequest xmlns:=\"http://www.edafa.com/web2sms/sms/model/\""+
                                                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
                                                "xsi:schemaLocation=\"http://www.edafa.com/web2sms/sms/model/ SMSAPI.xsd \" xsi:type=\"SubmitSMSRequest\">"+
                                                "<AccountId>"+AccountId+"</AccountId>"+
                                                "<Password>"+Password+"</Password>"+
                                                "<SecureHash>"+SecureHashSecretKey+"</SecureHash>"+
                                                "<SMSList>"+
                                                "<SenderName>"+SenderName+"</SenderName>"+
                                                "<ReceiverMSISDN>"+MSISDN+"</ReceiverMSISDN>"+
                                                "<SMSText>"+SMSMessage+"</SMSText>"+
                                                "</SMSList>"+
                                                "</SubmitSMSRequest>";
req = System.Net.WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
writer = new System.IO.StreamWriter(req.GetRequestStream());
writer.WriteLine (txtXMLData);
writer.Close ();

rsp = req.GetResponse();

Reader = new System.IO.StreamReader(rsp.GetResponseStream());
responseFromServer = Reader.ReadToEnd();

rsp.Close();
Reader.Close();


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}

当我尝试通过 "SMS API" 发送短信时,我遇到了以下异常 "Unsupported media type 415"。 提前致谢

尝试将 ContentType 更改为 application/xml

req.ContentType = "application/xml";

有时 APIs 可能对他们接受的内容非常严格。

我也会尝试设置接受 header,这会告诉 API 你也可以处理什么。

req.Accept = "application/xml";

试试这个对我来说效果很好@VodaFone

    var client = new HttpClient();
    var httpContent = new StringContent(txtXMLData, Encoding.UTF8, "application/xml");
    var respone = await client.PostAsync(APIUrl, httpContent);