json call with C#

前端 未结 5 1112
醉话见心
醉话见心 2020-12-04 22:08

I am trying to make a json call using C#. I made a stab at creating a call, but it did not work:

public bool SendAnSMSMessage(string message)
{
    HttpWebR         


        
5条回答
  •  抹茶落季
    2020-12-04 22:42

    Its just a sample of how to post Json data and get Json data to/from a Rest API in BIDS 2008 using System.Net.WebRequest and without using newtonsoft. This is just a sample code and definitely can be fine tuned (well tested and it works and serves my test purpose like a charm). Its just to give you an Idea. I wanted this thread but couldn't find hence posting this.These were my major sources from where I pulled this. Link 1 and Link 2

    Code that works(unit tested)

               //Get Example
                var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://abc.def.org/testAPI/api/TestFile");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "GET";
    
                var username = "usernameForYourApi";
                var password = "passwordForYourApi";
    
                var bytes = Encoding.UTF8.GetBytes(username + ":" + password);
                httpWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(bytes));
                var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string result = streamReader.ReadToEnd();
                    Dts.Events.FireInformation(3, "result from readng stream", result, "", 0, ref fireagain);
                }
    
    
                //Post Example
                var httpWebRequestPost = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://abc.def.org/testAPI/api/TestFile");
                httpWebRequestPost.ContentType = "application/json";
                httpWebRequestPost.Method = "POST";
                bytes = Encoding.UTF8.GetBytes(username + ":" + password);
                httpWebRequestPost.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(bytes));
    
                //POST DATA newtonsoft didnt worked with BIDS 2008 in this test package
                //json https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net
                // fill File model with some test data
                CSharpComplexClass fileModel = new CSharpComplexClass();
                fileModel.CarrierID = 2;
                fileModel.InvoiceFileDate = DateTime.Now;
                fileModel.EntryMethodID = EntryMethod.Manual;
                fileModel.InvoiceFileStatusID = FileStatus.NeedsReview;
                fileModel.CreateUserID = "37f18f01-da45-4d7c-a586-97a0277440ef";
                string json = new JavaScriptSerializer().Serialize(fileModel);
                Dts.Events.FireInformation(3, "reached json", json, "", 0, ref fireagain);
                byte[] byteArray = Encoding.UTF8.GetBytes(json);
                httpWebRequestPost.ContentLength = byteArray.Length;
                // Get the request stream.  
                Stream dataStream = httpWebRequestPost.GetRequestStream();
                // Write the data to the request stream.  
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.  
                dataStream.Close();
                // Get the response.  
                WebResponse response = httpWebRequestPost.GetResponse();
                // Display the status.  
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                Dts.Events.FireInformation(3, "Display the status", ((HttpWebResponse)response).StatusDescription, "", 0, ref fireagain);
                // Get the stream containing content returned by the server.  
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.  
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.  
                string responseFromServer = reader.ReadToEnd();
                Dts.Events.FireInformation(3, "responseFromServer ", responseFromServer, "", 0, ref fireagain);
    

    References in my test script task inside BIDS 2008(having SP1 and 3.5 framework)

提交回复
热议问题