Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
Hi am caling this below but i am getting error can u guide me

sservice
[OperationContract]
[WebInvoke(UriTemplate = "/AddBook/?name={name}", Method = "PUT")]
string AddBook(string name);


client

string uri = "http://localhost:53215/IBookService.svc/AddBook/?name=wcf";
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.KeepAlive = false;

//req.ContentLength = 0;
//req.ContentType = "text/json";
//Stream data = req.GetRequestStream();
//data.Close();

string result;

using (WebResponse resp = req.GetResponse())
{
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}

result = result.Substring(1, result.Length - 2);
Posted

Looks like you are sending a GET request to the server, but the method is defined as PUT. Hence add and try again:
C#
req.Method = "PUT";
 
Share this answer
 
//Name spaces
C#
using System;
using System.Text;
using System.Net;
using System.IO;


// ===========================================
 try
            {
                string WebServiceURL = tbWebServiceURL.Text; // store Url Of service in string

                // Convert our JSON in into bytes using ascii encoding
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(tbJSONdata.Text);

                //  HttpWebRequest 
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(WebServiceURL);
                webrequest.Method = "POST";
                webrequest.ContentType = "application/x-www-form-urlencoded";
                webrequest.ContentLength = data.Length;

                //  Get stream data out of webrequest object
                Stream newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                //  Declare & read the response from service
                HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

                // Fetch the response from the POST web service
                Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
                string result = loResponseStream.ReadToEnd();
                loResponseStream.Close();

                webresponse.Close();

                txtResult.Text = result;
            }
            catch (Exception ex)
            {
                txtResult.Text = "An exception was thrown: " + ex.Message;
            }
 
Share this answer
 

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