获取 POST 端点以在自托管 (WebServiceHost) C# web 服务中工作? [英] Getting a POST endpoint to work in self-hosted (WebServiceHost) C# webservice?
问题描述
所以,我一直在搞网络服务有一段时间了,我一直在回归一些基础知识,但我似乎从来没有做对.
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/etc 定义方法/端点.设置 GET 方法很容易,而且它的工作方式非常直接,而且很容易理解它的工作原理.例如:
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).
但是,当我将其定义为 POST 时,这意味着什么?
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),以便在服务器端正确解释它?
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 是如何影响事物的?我到处都读到了这方面的内容(MSDN、Stackoverflow 等),但还没有找到明确而好的答案.)
(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);
然后我尝试使用 Fiddler 调用此方法,但这根本不起作用.我刚收到一个 400 错误,说HTTP/1.1 400 错误请求".我在方法代码的第一行有一个断点,方法本身不包含任何内容:
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 如何知道我使用 Fiddler 发布的内容应该包含在字符串 pc"中? 它如何将其解释为字符串,以及什么类型的字符串(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)?
这是从 Fiddler 发送的 RAW 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
就我所见,我使用什么类型的内容类型并不重要.
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]
任何帮助将不胜感激.谢谢.
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屋!