Docs Rest API请求和响应

时间:2016-02-29 05:45:46

标签: c# httprequest httpresponse

此请求包含REST请求的测试数据并从Docsaway API获取响应。这解释了如何将Json对象绑定到REST API请求并在c#中获取json响应。将pdf文件作为附件发送到httprequest。

 public static void SendRequest()
    {
        Dictionary<string, string> Connectionchil = new Dictionary<string, string>();// Started to make request object for request with sample data
        Connectionchil.Add("email", "Your Email");
        Connectionchil.Add("key", "your key");

        Dictionary<string, string> recpientchild = new Dictionary<string, string>();
        recpientchild.Add("name", "testname");
        recpientchild.Add("company", "company");
        recpientchild.Add("address1", "Test Address One");
        recpientchild.Add("address2", "Test Address 2");
        recpientchild.Add("city", "Test City");
        recpientchild.Add("state", "TestStae");
        recpientchild.Add("zip", "2260");
        recpientchild.Add("country", "AU");

        Dictionary<string, string> stationchild = new Dictionary<string, string>();
        stationchild.Add("id", "GB2");
        stationchild.Add("courierID", "GB2_2");
        stationchild.Add("ink", "BW");
        stationchild.Add("paper", "80");//End of request  object  for request with sample data

        FileStream fsAttachIn = System.IO.File.OpenRead("d:\\test.pdf");// 
        var streamOfFile = File.ReadAllBytes("d:\\test.pdf");
        string base64PDF = Convert.ToBase64String(streamOfFile);
        JsonReQuestObject RequesetObj = new JsonReQuestObject(Connectionchil, "TEST", true, recpientchild, stationchild, base64PDF, "Testing Seller");
        var Json = JsonConvert.SerializeObject(RequesetObj);//Serialization of request object into json object


        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.docsaway.com/app/api/rest/mail.json");// Started to make HTTP Request
        httpWebRequest.ContentType = "json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            streamWriter.Write(Json);
            streamWriter.Flush();
            streamWriter.Close();

        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        string result = null;

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) //
        {
            result = streamReader.ReadToEnd();
        }
        JsonResoponseOfRequest ResponseResult = JsonConvert.DeserializeObject<JsonResoponseOfRequest>(result);//Deserialization of  response into custom classes generated 

        Console.Write("API Error Number :" + ResponseResult.APIErrorNumber + "\n");
        Console.WriteLine("API Report start from below\n");
        Console.Write("\t" + ResponseResult.APIReport);
        Console.WriteLine("API Report Ends Here\n");
        Console.Write("Document Details starts here \n");
        foreach (var d in ResponseResult.Document)
        {
            Console.Write("\t" + d.Key + ":");
            Console.Write("\t" + d.Value + "\n");
        }
        Console.Write("Document Details ends here\n");

        Console.WriteLine("Station information starts from here\n");
        foreach (var s in ResponseResult.station)
        {
            Console.Write("\t" + s.Key + ":");
            Console.Write("\t" + s.Value + "\n");
        }
        Console.WriteLine("station information ends here");

        Console.WriteLine("Transaction information starts here");
        foreach (var t in ResponseResult.transaction)
        {
            Console.Write("\t" + t.Key + ":");
            Console.Write("\t" + t.Value + "\n");
        }
        Console.WriteLine("Transaction information Ends here");
        Console.ReadLine();
    }

0 个答案:

没有答案
相关问题