【发布时间】:2012-07-24 05:35:07
【问题描述】:
我正在使用 SendGrid 电子邮件服务通过 Web API (HttpWebRequest) 发送电子邮件。这是正在使用的代码,但我收到了 400 响应。
string url = "https://sendgrid.com/api/mail.send.xml";
string parameters = "api_user=" + api_user + "&api_key=" + api_key + "&to=" + toAddress + "&toname=" + toName + "&subject=" + subject + "&text=" + text + "&from=" + fromAddress;
// Create Request
myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myHttpWebRequest.Method = "POST";
// myHttpWebRequest.ContentType = "application/json; charset=utf-8";
myHttpWebRequest.ContentType = "text/xml";
CookieContainer cc = new CookieContainer();
myHttpWebRequest.CookieContainer = cc;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] postByteArray = encoding.GetBytes(parameters);
myHttpWebRequest.ContentLength = postByteArray.Length;
System.IO.Stream postStream = myHttpWebRequest.GetRequestStream();
postStream.Write(postByteArray, 0, postByteArray.Length);
postStream.Close();
// Get Response
string result="";
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
【问题讨论】: