获得一个POST端点在自托管(WebServiceHost)C#web服务工作? [英] Getting a POST endpoint to work in self-hosted (WebServiceHost) C# webservice?

查看:1636
本文介绍了获得一个POST端点在自托管(WebServiceHost)C#web服务工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在与web服务,而现在乱搞,和我不断收到回一些基础知识,我似乎从来没有得到正确的。

So, I have been messing around with webservices for a while now, and I keep getting back to some basics, that I never seem to get right.

在使用.NET / C#中的WebServiceHost,你可以定义一个方法/端点使用GET / POST /等。建立一个GET,方法简单,它的工作原理pretty直接得多,而且其很容易理解它是如何工作。例如:

When using a WebServiceHost in .NET/C#, you can define a method/endpoint as using GET/POST/etc. Setting up a GET-method is easy and it works pretty much directly, and its easy to understand how it works. For example:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/PutMessage/{jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string PutMessage(string jsonString);

如果我访问http:///为MyWebService / PUTMESSAGE / {} MyJsonString我获得通过的方法,一切都很好(或多或少)

If I call http:///MyWebService/PutMessage/{MyJsonString} I get passed on the the method, and all is well (more or less).

但随后这是什么意思,当我确定这是一个发表呢?

But then what does it mean when I define this as a POST instead?

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/PutMessage/{jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string PutMessage(string jsonString);

什么是UriTemplate在这里做?如果我做一个POST,我期望不包含在URI的数据,但在后的数据部分。但我定义数据段的变量名称?如何在WebServiceHost / .NET知道什么是包含在后的数据部分是要放入变量jsonString?我如何从发布客户端的数据(而不是C#,让我们说的JQuery代替),这样它在塞雷尔语边正确PTED间$ P $?

What does the UriTemplate do here? If I do a POST, I expect the data to be contained not in the URI, but in the "data section" of the post. But do I define the variable name in the data section? How does the WebServiceHost/.NET know that what is contained in the "data section" of the post is to be put into the variable jsonString? How do I post the data from the client side (not C#, let's say JQuery instead) so that it is interpreted correctly on the serer side?

(以及如何做的WebMessageFormat affet东西呢?我已阅读无处不在这个(MSDN,#2等),但还没有找到一个明确的和很好的答案。)

(And how does the WebMessageFormat affet things? I have read everywhere about this (MSDN, Stackoverflow etc) but haven't found a clear and good answer.)

在我试图了解这一点,我想我会做一个很简单的POST方法,是这样的:

In my attempts to understand this, I thought I'd make a very simple POST-method, like this:

[OperationContract]
[WebInvoke]
string PutJSONRequest(string pc);

然后我的尝试的调用使用招这个方法,但是,这并不在所有的工作。我只是得到一个400错误回来,说:HTTP / 1.1 400错误的请求。我在这个方法中的code中的第一行断点,方法本身不包含任何内容:

I then try call this method using Fiddler, but that does not work at all. I just get a 400 error back, saying "HTTP/1.1 400 Bad Request". I have a breakpoint on the very first line in the code of the method, and the method itself contains nothing:

public string PutJSONRequest(string pc)
{
    return null;
}

此外,如何.NET知道什么我张贴使用招应包含在串PC?如何做它跨preT它作为一个字符串,什么类型的字符串(UT8,ASCII等)?

Again, how does .NET know that what I POSTed using Fiddler should be contained in the "string pc"? How does it interpret it as a string, and what type of string (UT8, ASCII etc)?

这是原始的HTTP请求,从提琴手发送:

This is the RAW HTTP request, sent from Fiddler:

POST http://<myip>:8093/AlfaCustomerApp/PutJSONRequest HTTP/1.1
User-Agent: Fiddler
Host: <myip>:8093
Content-Length: 3
Content-type: application/x-www-form-urlencoded; charset=UTF-8

asd

和它不事关什么类型的Content-type我使用的,据我所看到的。

and it doesnt matter what type of Content-type I use, as far as I can see.

响应是一个标准的事情,我不是在自己的控制:

The response is a standard-thing, that I am not in control of myself:

HTTP/1.1 400 Bad Request
Content-Length: 1165
Content-Type: text/html
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 15 Oct 2012 15:45:02 GMT

[then HTML code]

任何帮助将是AP preciated。谢谢你。

Any help would be appreciated. Thanks.

推荐答案

我找到了答案。这是如何定义的方法,即可以在一个HTTP POST发送的原始数据:

I found out the answer. This is how to define a method, that can take the raw data sent in a HTTP POST:

[OperationContract]
[WebInvoke(BodyStyle=WebMessageBodyStyle.Bare)]
Stream PutMessage(Stream data);

和实施是这样的:

public Stream PutMessage(Stream data)
{
    byte[] buffer = new byte[65535];

    int bytesRead, totalBytes = 0;
    do
    {
        bytesRead = data.Read(buffer, 0, 65535);
        totalBytes += bytesRead;
    }
    while (bytesRead > 0);

    // Then you could interpret it as a String for example:
    string jsonString = Encoding.UTF8.GetString(buffer, 0, totalBytes);
    // yada yada
}

这篇关于获得一个POST端点在自托管(WebServiceHost)C#web服务工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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