如何使用ASP.net在表中存储sendgrid的响应? [英] How can I store response from sendgrid in table using ASP.net?

查看:41
本文介绍了如何使用ASP.net在表中存储sendgrid的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sendgrid API发送和检索已发送邮件的统计信息。我想将API的响应存储在数据库中。

  protected   void  btnBounces_Click( object  sender,EventArgs e)
{
try
{
string url = https://api.sendgrid.com/api/bounces.get.json;
GetResult(url);
}
catch (例外情况)
{
lblError.Text = ex.Message.ToString();
}
}
public void GetResult( string url)
{
string parameters = api_user = xxxx& api_key = xxxx& date = 1& start_date = + txtStartDate.Text + & end_date = + txtEndDate.Text;
// 启动请求
HttpWebRequest req =(HttpWebRequest)HttpWebRequest.Create (URL);
req.Method = POST;
req.ContentType = application / x-www-form-urlencoded;
StreamWriter streamWriter = new StreamWriter(req.GetRequestStream());
streamWriter.Write(参数);
streamWriter.Flush();
streamWriter.Close();
// 获取回复
HttpWebResponse res =(HttpWebResponse)req.GetResponse ();
StreamReader streamReader = new StreamReader(res.GetResponseStream());
string result = streamReader.ReadToEnd();
}



我将得到的回复如下:

[{status :4.0.0,created:2011-09-16 22:02:19,reason:无法解析MX主机sendgrid.ne,email:esting@sendgrid.ne },{status:4.0.0,created:2011-09-19 17:47:15,reason:连接超时,email:rawest@gmail.co }}

如何提取四个字段中每个字段的值并将它们存储在包含四个字段的表中?

解决方案

我得到了答案



  public   class  BouncesAndBlocks 
{
public string status { get ; set ; }
public string created { get ; set ; }
public string email { get ; set ; }
public string 原因{ get ; set ; }
}
受保护 无效 btnBounces_Click( object sender,EventArgs e)
{
string url = https://api.sendgrid.com/api/bounces.get.json;
string JS = GetResult(url);
列表< bouncesandblocks> res =(List< bouncesandblocks>)JsonConvert.DeserializeObject(JS, typeof (List< bouncesandblocks>));
foreach var item in res)
{
// 存储在数据库中
lbl .Text = 日期: + item.created.ToString()+ 状态: + item.status.ToString()+ 电子邮件: + item.email.ToString()+ 消息: + item.reason.ToString()+ ;

}
}
< / bouncesandblocks > < / bouncesandblocks > < / bouncesandblocks >


I am using Sendgrid API to send and retrieve statistics of mail sent. I want to store the response of API in database.

protected void btnBounces_Click(object sender, EventArgs e)
{
    try
    {
        string url = "https://api.sendgrid.com/api/bounces.get.json";
        GetResult(url);
    }
    catch (Exception ex)
    {
        lblError.Text = ex.Message.ToString();
    }
}
 public void GetResult(string url)
{
    string parameters = "api_user=xxxx&api_key=xxxx&date=1&start_date="+txtStartDate.Text+"&end_date="+txtEndDate.Text;
    // Start the request
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    StreamWriter streamWriter = new StreamWriter(req.GetRequestStream());
    streamWriter.Write(parameters);
    streamWriter.Flush();
    streamWriter.Close();
    // Get the response
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    StreamReader streamReader = new StreamReader(res.GetResponseStream());
    string result = streamReader.ReadToEnd();
}


The response I will get will be like:

[ { "status": "4.0.0", "created": "2011-09-16 22:02:19", "reason": "Unable to resolve MX host sendgrid.ne", "email": "esting@sendgrid.ne" }, { "status": "4.0.0", "created": "2011-09-19 17:47:15", "reason": "Connection timed out", "email": "rawest@gmail.co" } }

How can i extract value of each of the four fields and store them in table containing four fields?

解决方案

I got the answer

public class BouncesAndBlocks
{
    public string status { get; set; }
    public string created { get; set; }
    public string email { get; set; }
    public string reason { get; set; }
}
     protected void btnBounces_Click(object sender, EventArgs e)
{
    string url = "https://api.sendgrid.com/api/bounces.get.json";
    string JS = GetResult(url);
    List<bouncesandblocks> res = (List<bouncesandblocks>)JsonConvert.DeserializeObject(JS, typeof(List<bouncesandblocks>));
    foreach (var item in res)
    {
        //Store in database
        lbl.Text = "Date:" + item.created.ToString() + "    Status:" + item.status.ToString() + "     Email:" + item.email.ToString() + "       Message:" + item.reason.ToString() + "";

    }
}
</bouncesandblocks></bouncesandblocks></bouncesandblocks>


这篇关于如何使用ASP.net在表中存储sendgrid的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆