WCF远程服务器返回错误(404)未找到?

时间:2017-04-04 20:13:06

标签: c# rest wcf post

我有一个在VS2015中启动的WCF服务。然后客户端代码在另一个VS2015 ..我不断得到"远程服务器返回错误(404)Not Found"错误。当我将参数添加到网址" http://localhost:53989/FileShareService.svc/UploadFile/WTM"工作正常,但如果我添加为encoding.GetBytes我得到错误。

int a = A::B::Val1;

客户端代码不起作用:

[ServiceContract]
public interface IFileShareWebService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "UploadFile/{patron}",
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json,
               BodyStyle = WebMessageBodyStyle.Bare)]
    void UploadFile(string patron);

}

客户端代码工作:

     ASCIIEncoding encoding = new ASCIIEncoding();

        string postData = "patron=WTM";
        byte[] data = encoding.GetBytes(postData);

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:53989/FileShareService.svc/UploadFile/WTM");
        httpWebRequest.ContentType = "application/json; charset=utf-8";
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentLength = 0;
        //httpWebRequest.GetRequestStream();
        // Code to write data to the stream
        using (Stream requestStream = httpWebRequest.GetRequestStream())
        {
            requestStream.Write(data, 0, data.Length);
        }

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

0 个答案:

没有答案
相关问题