1

それで、私はしばらくの間Webサービスをいじり回していて、私はいくつかの基本に立ち返り続けていますが、私は決して正しくないようです。

質問1:

.NET / C#でWebServiceHostを使用する場合、メソッド/エンドポイントをGET / POST/etcを使用するように定義できます。GETメソッドの設定は簡単で、ほとんど直接機能し、その機能を理解するのも簡単です。例えば:

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

http:/// MyWebService / PutMessage / {MyJsonString}を呼び出すと、メソッドが渡され、すべてが順調です(多かれ少なかれ)。

しかし、これを代わりにPOSTとして定義するとはどういう意味ですか?

[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としましょう)からデータを投稿して、サーバー側で正しく解釈されるようにするにはどうすればよいですか?

(そして、WebMessageFormatはどのように物事に影響を与えますか?私はこれについてどこでも読んだことがあります(MSDN、Stackoverflowなど)が、明確で良い答えを見つけられませんでした。)

質問2:

これを理解するために、次のような非常に単純なPOSTメソッドを作成すると思いました。

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

次に、Fiddlerを使用してこのメ​​ソッドを呼び出してますが、それはまったく機能しません。「HTTP/1.1400BadRequest」という400エラーが返されます。メソッドのコードの最初の行にブレークポイントがあり、メソッド自体には何も含まれていません。

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

繰り返しになりますが、.NETは、Fiddlerを使用して投稿したものが「文字列pc」に含まれている必要があることをどのように認識しますか?それを文字列としてどのように解釈し、どのタイプの文字列(UT8、ASCIIなど)ですか?

これは、Fiddlerから送信されたRAWHTTPリクエストです。

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

そして、私が見る限り、どのタイプのコンテンツタイプを使用するかは問題ではありません。

応答は標準的なものであり、私は自分自身を制御できません。

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]

どんな助けでもいただければ幸いです。ありがとう。

4

2 に答える 2

5

簡単なコードですべての質問に答えられると思います

Task.Factory.StartNew(()=>StartServer());
Thread.Yield();
StartClient();

void StartServer()
{
    Uri uri = new Uri("http://localhost:8080/test");
    WebServiceHost host = new WebServiceHost(typeof(WCFTestServer), uri);
    host.Open();
}

void StartClient()
{
    try
    {
        WebClient wc = new WebClient();

        //GET
        string response1 = wc.DownloadString("http://localhost:8080/test/PutMessageGET/abcdef");
        //returns: "fedcba"

        //POST with UriTemplate
        string response2 = wc.UploadString("http://localhost:8080/test/PutMessagePOSTUriTemplate/abcdef",
                                            JsonConvert.SerializeObject(new { str = "12345" }));
        //returns: fedcba NOT 54321


        //POST with BodyStyle=WebMessageBodyStyle.WrappedRequest
        //Request: {"str":"12345"}
        wc.Headers["Content-Type"] = "application/json";
        string response3 = wc.UploadString("http://localhost:8080/test/PutMessagePOSTWrappedRequest",
                                            JsonConvert.SerializeObject(new { str="12345" }));

        //POST with BodyStyle=WebMessageBodyStyle.Bare
        wc.Headers["Content-Type"] = "application/json";
        string response4 = wc.UploadString("http://localhost:8080/test/PutMessagePOSTBare", "12345" );

    }
    catch (WebException wex)
    {
        Console.WriteLine(wex.Message);
    }
}

[ServiceContract]
public class WCFTestServer
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/PutMessageGET/{str}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string PutMessageGET(string str)
    {
        return String.Join("", str.Reverse());
    }

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/PutMessagePOSTUriTemplate/{str}", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string PutMessagePOSTUriTemplate(string str)
    {
        return String.Join("", str.Reverse());
    }

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string PutMessagePOSTWrappedRequest(string str)
    {
        return String.Join("", str.Reverse());
    }

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string PutMessagePOSTBare(string str)
    {
        return String.Join("", str.Reverse());
    }
}

PS:ここでJsonConvertを見つけることができます

于 2012-10-15T18:34:08.530 に答える
3

私は答えを見つけました。これは、HTTPPOSTで送信された生データを取得できるメソッドを定義する方法です。

[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
}
于 2012-11-29T21:10:28.393 に答える