Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am consuming a REST WCF service in console application and want to display all information according to URL,BODY,METHOD like below.

REST Service URL:
http://localhost/TAExcelApps/TemplateService/GetTemplatesInfos[^]

BODY :
<ArrayOfTemplateInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><TemplateInfo><UserID>17</UserID></TemplateInfo></ArrayOfTemplateInfo>

Method :POST
////////////////////////////////////

My coding is below which is not working:

error:The remote server returned an error: (404) Not Found.

C#
try
            {
                // Create URI
                string uri = "http://localhost/TAExcelApps/TemplateService/GetTemplatesInfos";
                string gggg = "<arrayoftemplateinfo>";
                string body1 = gggg+"<templateinfo>"+
                          "<userid>254</userid>"+
                          "</templateinfo>"+
                          "</arrayoftemplateinfo>";
                  string body=body1.ToString();
                Uri address = new Uri(uri.ToString());

                // Create the web request 
                HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

                // Set type to POST 
                request.Method = "POST";
                request.KeepAlive = true;
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Headers.Add("Key1", "Value1");
              
                // According to the requested content type, the request and response content type can be set
               
                    request.ContentType = "text/xml";
                    request.Accept = "text/xml";
                

                // HttpResponse httpResponse = client.execute(httpPost);
                byte[] byteData = UTF8Encoding.UTF8.GetBytes(body);

                //Set the content length in the request headers 
                request.ContentLength = byteData.Length;

                //-- Write data   --//
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(byteData, 0, byteData.Length);
                }

                // Get response 
                using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse)
                {
                    Stream ResponseStream = null;
                    ResponseStream = httpResponse.GetResponseStream();
                    int responseCode = (int)httpResponse.StatusCode;
                    string responseBody = ((new StreamReader(ResponseStream)).ReadToEnd());
                    string contentType = request.ContentType;
                }

            }
            catch (WebException Ex)
            {
                if (Ex.Status == WebExceptionStatus.ProtocolError)
                {
                    int StatusCode = (int)((HttpWebResponse)Ex.Response).StatusCode;
                    Stream ResponseStream = null;
                    ResponseStream = ((HttpWebResponse)Ex.Response).GetResponseStream();
                    string responseText = (new StreamReader(ResponseStream)).ReadToEnd();
                    if (StatusCode == 500)
                    {
                        // Do Something
                    }
                    else
                    {
                        // Do Something for other status codes
                    }
                }
                else
                {
                    throw (Ex); // Or check for other WebExceptionStatus
                }
            }
Posted
Updated 3-Sep-13 22:24pm
v2
Comments
ArunRajendra 4-Sep-13 3:56am    
Check the request URL in IIS log.

1 solution

string postData = "&lt;TemplateInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance'&gt;" + "&lt;UserID&gt;254&lt;/UserID&gt;" + "&lt;/TemplateInfo&gt;";

C#
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

           //create an HTTP request to the URL that we need to invoke

           HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://172.19.18.126/TAExcelApps/TemplateService/GetTemplatesInfos");

           request.ContentLength = byteArray.Length;
           request.ContentType = "application/xml"; //set the content type to XML
           request.Method = "POST"; //make an HTTP POST

           using (Stream dataStream = request.GetRequestStream())
           {

               //initiate the request
               dataStream.Write(byteArray, 0, byteArray.Length);
           }

           // Get the response.

           using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse)
           {
               Stream ResponseStream = null;
               ResponseStream = httpResponse.GetResponseStream();
               int responseCode = (int)httpResponse.StatusCode;
               string responseBody = ((new StreamReader(ResponseStream)).ReadToEnd());
               Console.WriteLine(responseBody.ToString());
           }

           Console.ReadLine();
 
Share this answer
 
v2

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