如何使用c#发送POST变量?

时间:2011-06-21 14:00:30

标签: c# wcf post

我有一个WCF REST服务,并且我试图手动将一些数据发送到它,以便WCF方法将它作为其参数,但在POST c#中填充变量的位置时遇到问题。 WCF方法是:

[OperationContract]
[WebInvoke]
string EchoWithPost(string message);

到目前为止,我已经从MSDN网站上删除了一些脚本:

            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://localhost:52587/VLSContentService.svc/rest/EchoWithGet/Hello%20World");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

我真的不确定如何将脚本转换为一些代码,这些代码会将'消息'发送到WCF服务。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

我相信正确的Postdata将是message=YOUR_ESCAPED_POSTDATA&additional=arguments,就像GET查询一样。

例如,您可以下载像firebug这样的插件并提交常规表单。然后你可以看到浏览器如何发送它的后期数据并只是复制它。

答案 1 :(得分:1)

WCF REST端点默认情况下理解两种数据:XML和JSON,因此下面显示的两种方法都可以正常工作,因为操作需要string

string postData = "\"This is a test that posts this string to a Web server.\"";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/json;

string postData = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">This is a test that posts this string to a Web server.</string>";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "text/xml;

WCF不支持内容类型application / x-www-form-urlencoded(但如果你从http://wcf.codeplex.com/获得“jQuery支持”,你可以找到一个行为,支持它)。如果您想接收任何类型的数据,包括非结构化数据(例如纯文本),您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx找到更多信息。