What is the correct URI for sending parameters through POST in WCF REST services?

Let's say that I specified the following WCF REST service at http: //localhost/MyRESTService/MyRESTService.svc "

[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/receive")]
string Receive(string text);

Now I can call the REST service in Fiddler using the address " http: //localhost/MyRESTService/MyRESTService.svc/receive " and it works (I will get the return value).

But what if I want to send parameters to my REST service? Should I change the interface definition to:

[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/receive/{text}")]
string Receive(string text);

, REST Fiddler, " http://localhost/MyRESTService/MyRESTService.svc/receive/mytext" ( "mytext", ). URI POST?

, , , URI , . , POST WCF REST, , URI.

Dictionary<string, string> postDataDictionary = new Dictionary<string, string>();
      postDataDictionary.Add("text", "mytext");

      string postData = "";
      foreach (KeyValuePair<string, string> kvp in postDataDictionary)
      {
        postData += string.Format("{0}={1}&", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
      }
      postData = postData.Remove(postData.Length - 1); 

      Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive");
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
      req.Method = "POST";
      byte[] postArray = Encoding.UTF8.GetBytes(postData);
      req.ContentType = "application/x-www-form-urlencoded";
      req.ContentLength = postArray.Length;

      Stream dataStream = req.GetRequestStream();
      dataStream.Write(postArray, 0, postArray.Length);
      dataStream.Close();

      HttpWebResponse response = (HttpWebResponse)req.GetResponse();
      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader(responseStream);

      string responseString = reader.ReadToEnd();

      reader.Close();
      responseStream.Close();
      response.Close();

(, "mytext" ) POST, URI

Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive");

( , , , URI)

Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive/mytext");

, , WCF REST Services.

+3
2

, , XML, WCF REST ( ), . , , , Google, URI (, - XML, URI). , , , , WCF , "400 Bad Request". , WCF XML, , - (, Microsoft?) .NET. ). , ​​ ).

** IMyRESTService.cs( ) **

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
Stream Receive(Stream text);

** **

XmlDocument MyXmlDocument = new XmlDocument();
MyXmlDocument.Load(FilePath);
byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);

Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/Receive");

Request.ContentLength = RequestBytes.Length;

Request.Method = "POST";

Request.ContentType = "text/xml";

Stream RequestStream = Request.GetRequestStream();
RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
RequestStream.Close();

HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ResponseMessage = reader.ReadToEnd();
response.Close();

** XmlContentTypeMapper.cs( , WCF XML) **

public class XmlContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}

** Web.config( )

<endpoint binding="customBinding" bindingConfiguration="XmlMapper" contract="MyRESTService.IMyRESTService"
           behaviorConfiguration="webHttp"    />

<bindings>
  <customBinding>
    <binding name="XmlMapper">
      <webMessageEncoding webContentTypeMapperType="MyRESTService.XmlContentTypeMapper, MyRESTService"/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>
</bindings>

- WCF HTTP POST http://social.msdn.microsoft.com/forums/en-us/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC

+4

, Fiddler 403 (Bad Request) , Body. , , - /xml Xml.

, Fiddler:

Content-Type: application/xml;charset=UTF-8 

Microsoft.Http, WebRequest , Content-Type :

request.ContentType = "application/xml;charset=UTF-8";

.

, URI , WebGet WebInvoke POST.

+2

Source: https://habr.com/ru/post/1757359/


All Articles