开发者

How do I send POST variables with c#?

开发者 https://www.devze.com 2023-03-14 15:18 出处:网络
I have a WCF REST service and im trying to manually POST some data to it so that the WCF methood gets it as its argument but having trouble working out where in the POST c# to stuff the variable. The

I have a WCF REST service and im trying to manually POST some data to it so that the WCF methood gets it as its argument but having trouble working out where in the POST c# to stuff the variable. The WCF method is:

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

And so far I have taken some script off the MSDN site:

            // 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(((HttpWebRe开发者_JS百科sponse)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();

Im not really sure how to translate the script into some code that will POST 'message' to the WCF service. Can anybody help?


WCF REST endpoints understand by default two kinds of data: XML and JSON, so both ways shown below should work fine, since the operation expects a 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;

and

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;

The content-type application/x-www-form-urlencoded is not supported out-of-the-box by WCF (but if you get the "jQuery support" from http://wcf.codeplex.com/ you can find a behavior which supports it). And if you want to receive any kind of data, included unstructured ones (such as plain text), you can find more information at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx.


I believe the correct Postdata would be message=YOUR_ESCAPED_POSTDATA&additional=arguments, just like a GET query looks like.

You could for example download an addon like firebug and submit a regular form. Then you can see how the browser sends its post-data and just replicate that.

0

精彩评论

暂无评论...
验证码 换一张
取 消