为什么我没有收到RestRequest的IRestResponse?

时间:2018-05-10 15:54:13

标签: c# excel restsharp

我对这个特定的编程领域很陌生(所以我可以随意为我做些蠢事。)

基本上,我要提取一个包含产品信息的excel文件,循环并重新格式化数据,然后将数据发送到SureDone的API。

我正在读数据,所以格式化不是问题,但是,就API的问题而言,我显然遗漏了一些东西,因为我没有在txtResponse文本框中返回任何内容。

我可能会错过一个整体概念,我不确定我应该接受什么样的回应。我几乎从头开始,对所有这些工作原理知之甚少,并且可以真正使用任何指导。

这是我的代码:

private void btnLoad_Click(object sender, EventArgs e)
{
    string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtChooseFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
    OleDbConnection conn = new OleDbConnection(PathConn);

    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + txtLoad.Text + "$]", conn);
    DataTable dt = new DataTable();

    da.Fill(dt);

    dataGridView.DataSource = dt;

    String payload = "";
    int i = 1;

    foreach(DataRow row in dt.Rows)
    {
        //REST request -------------------------------------------------------------------------------------------------------
        var client = new RestClient("https://www.api.suredone.com/v1/editor/items/add");
        var request = new RestRequest(Method.POST);
        request.AddHeader("x-auth-token", "tokenString");
        request.AddHeader("x-auth-user", "username");
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        //--------------------------------------------------------------------------------------------------------------------

        Hashtable postData = new Hashtable();

        postData["action"] = row["action"].ToString();
        postData["sku"] = row["sku"].ToString();
        postData["stock"] = row["stock"].ToString();
        postData["price"] = row["price"].ToString();
        postData["msrp"] = row["msrp"].ToString();
        postData["title"] = row["title"].ToString();
        postData["longDescription"] = row["longdescription"].ToString();
        postData["condition"] = row["condition"].ToString();
        postData["brand"] = row["brand"].ToString();
        postData["notes"] = row["notes"].ToString();

        String formBoundary = "undefined";

        foreach (DictionaryEntry entry in postData)
        {    
            payload += "--" + formBoundary + "\r\n" + "Content-Disposition: form-data; name=" + entry.Key.ToString() + "\r\n\r\n" + entry.Value.ToString() + "\r\n\r\n";   
        }

        i++;

        //REST response -------------------------------------------------------------------------------------------
        request.AddParameter("application/x-www-form-urlencoded", payload, ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        txtResponse.Text = response.Content;
        //---------------------------------------------------------------------------------------------------------
    }

    txtProducts.Text = payload;   
}

0 个答案:

没有答案