远程服务器返回错误:(405)方法不允许。在wcf服务中消费 [英] The remote server returned an error: (405) Method Not Allowed. in wcf service consumming

查看:85
本文介绍了远程服务器返回错误:(405)方法不允许。在wcf服务中消费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好我在下面打电话但是我收到错误可以指导我



sservice

[OperationContract]

[WebInvoke(UriTemplate =/ AddBook /?name = {name},Method =PUT)]

string AddBook(string name );





客户



string uri =http:/ /lhosthost:53215/IBookService.svc/AddBook/?name=wcf;

HttpWebRequest req = WebRequest.Create(uri)as HttpWebRequest;

req.KeepAlive = false ;



//req.ContentLength = 0;

//req.ContentType =text / json;

//流数据= req.GetRequestStream();

//data.Close();



字符串结果; <使用(WebResponse resp = req.GetResponse())

使用(StreamReader reader = new StreamReader) ( resp.GetResponseStream()))

{

result = reader.ReadToEnd();

}

}



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

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);

推荐答案

看起来像您正在向服务器发送 GET 请求,但该方法定义为 PUT 。因此,添加并重试:

Looks like you are sending a GET request to the server, but the method is defined as PUT. Hence add and try again:
req.Method = "PUT";


//名称空间

//Name spaces
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;
            }


这篇关于远程服务器返回错误:(405)方法不允许。在wcf服务中消费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆