[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

posting data to a rest service rooke help

Hi EE,
I am trying to post xml data to a REST service using the following code.  I found this tutorial which I have pretty much copied from start to finish to see if it works.  

The tutorial is available here: http://devlicio.us/blogs/derik_whittaker/archive/2009/02/14/posting-data-to-a-rest-service-using-c.aspx

Although the code compiles im not sure how to call it, can EE advise me how to get this finished?

Many thanks as always,

    class Program
    {
        static void Main(string[] args)
        {
            var formData = obj.GetDictionary();
            obj.SendMessage(urlToRestService, formData);
        }
    }
 
 
        public static Dictionary<String, String> GetDictionary()        
        {
            string[] userIDs = Configuration.GetValues();
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("userID", userIDs[0]);
            return d;
        }
 
 
 
// tutorial code to send the data
 
        public static HttpWebRequest CreateWebRequest(string endPoint, Int32 contentLength)
        {
            var request = (HttpWebRequest)WebRequest.Create(endPoint);
            request.Method = Configuration.GetContentType();
            request.ContentLength = contentLength;
            request.ContentType = "application/x-www-form-urlencoded";
            return request;
        }
 
        public static string CreateFormattedPostRequest(ICollection values)  
        {
            var paramterBuilder = new StringBuilder();
            var counter = 0;
 
            foreach(var value in values)
            {
                paramterBuilder.AppendFormat("{0}={1}", value, HttpUtility.UrlDecode(value));
                if (counter != values.Count - 1)
                {
                    paramterBuilder.Append("&");
                }
                counter++;
            }
            return paramterBuilder.ToString();
        }
 
        public static void SendMessage(string endPoint, Dictionary<string, string> paramters)
        {
            var populatedEndPoint = CreateFormattedPostRequest(paramters);
            byte[] bytes = Encoding.UTF8.GetBytes(populatedEndPoint);
 
            HttpWebRequest request = CreateWebRequest(endPoint, bytes.Length);
 
            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }
            }
        }

Open in new window

0
jimbona27
Asked:
jimbona27
  • 2
1 Solution
 
Anurag ThakurCommented:
as per the sample code that you are following  SendMessage method is used to invoke the web service call

var formData = obj.GetDictionary();
obj.SendMessage(urlToRestService, formData);

how is the obj initialized and filled value with
if you are executing the code as shown in the mail then obj will always be blank
0
 
jimbona27Author Commented:
I have just added the following code to consume the wsdl service although I get object not set on the line that tries to set the owner



            Int32 counter = 0;
            string[] codes = Configuration.GetCodes();
            ServiceReference1.uploadRequest webService = new aaa.bbb.ServiceReference1.uploadRequest();
 
            foreach (string sCode in codes)
            {
                webService.uploadRequest1.owner = codes[counter];  // runtime error -- object not set
                webService.uploadRequest1.authoritative = true;
                webService.uploadRequest1.authoritativeSpecified = true;
                counter++;
            }

Open in new window

0
 
jimbona27Author Commented:
i have successfully consumed the service now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now