Click here to Skip to main content
15,881,801 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have developed courier tracking system,
in i have to insert only tracking number and it detects courier,
After that i have to pass json body to post courier ,
Can any one suggest regarding that???
Solutions will be highly appreciable and thanks in advance..

Here is the code below.. :(


C#
 if (TrackingNumber != "")
                {

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.aftership.com/v3/trackings);
                    request.ContentType = "application/json";
                    request.Method = "POST";
                    request.Headers.Add("aftership-api-key:*************************");
                
                    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    if (response.StatusCode == HttpStatusCode.OK)
                    {

                        Stream receiveStream = response.GetResponseStream();
                        StreamReader readStream = null;

                        if (response.CharacterSet == "")
                            readStream = new StreamReader(receiveStream);
                        else
                            readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                        //  List<string> list = new List<string>();
                        string data = readStream.ReadToEnd();
.
.
.
.
.


And i want to pass json body like this... :(

C#
{
    "tracking": {
        "slug": "dhl",
        "tracking_number": "123456789",
        "title": "Title Name",
    }
}


Here only tracking_number is mandatory ,else all fields are optional..... :(
Posted
Updated 9-Mar-21 17:51pm

i have solved it my self.. :)

C#
try
       {

           var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.aftership.com/v3/trackings");

           httpWebRequest.Method = "POST";

           httpWebRequest.Headers.Add("aftership-api-key:********fdbfd93980b8c5***");
           httpWebRequest.ContentType = "application/json";
           using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
           {

               string json = "{" + "\"tracking\": {" +"\"slug\":\""+Courier+"\","+"\"tracking_number\":\""+trackNumber+"\"}}";

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

               var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
               using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
               {
                   var result = streamReader.ReadToEnd();
               }
           }


       }
       catch
       {

       }
 
Share this answer
 
Comments
Volynsky Alex 5-Apr-14 11:17am    
Good answer!
Nirav Prabtani 6-Apr-14 1:17am    
Thank you.. :)
thanks brother it helps me a lot
 
Share this answer
 
Comments
CHill60 10-Mar-21 5:37am    
If you want to comment on a post please use the "Have a Question or Comment?" link next to it. Don't post comments as a solution - it just brings the post back into the list of active questions (and gains you downvotes and reports)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900