繁体   English   中英

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

[英]How do I send POST variables with c#?

我有一个WCF REST服务,我试图手动POST一些数据到它,以便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();

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM