locked
Send form data to WFC service RRS feed

  • Question

  • User-250408672 posted

    I current am taking the values from a form and adding them to my datacontract datamemembers using this code on the form post:

    InquiryData objIquiryData = new objInquiryData { FirstName = firstName, LastName = lastName, PhoneNumber = phoneNumber, Email = email, Interest = interest};

    Inquiry.cs which has the contract looks like this:

    [ServiceContract]
    public interface IJobInquiry
    {
    [OperationContract]
    [WebInvoke(Method = "Get", UriTemplate = "xml/{s}", BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    
    }
    
    [DataContract]
    public class InquiryData
    {
    [DataMember]
    public string FirstName { get; set; }
    
    [DataMember]
    public string LastName { get; set; }
    
    [DataMember]
    public string Email { get; set; }
    
    [DataMember]
    public string PhoneNumber { get; set; }
    
    [DataMember]
    public string Interest { get; set; }

    Since I am new to this, how to I send this object as XML to be sent to my localhost? The only end result is that the data in the object be serialized(i think) and sent to the WCF service.  Any ideas on a proper way to do this?

    Monday, June 2, 2014 10:16 PM

Answers

  • User-417640953 posted

    Hi prescottvall,

    Thank you post the issue to asp.net forum.

    According to your description and code provided, I see you want to post xml data to wcf rest service.

    As for you just pass a single parameter "InquiryData" to rest method. So I suggest you set the BodyStyle as WebMessageBodyStyle.bare.

    And the Method should be "Post".

    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "/XMLDataPost", BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    public void XMLDataPost(InquiryData inquerydata);
           

    Then you can post xml to it like below.

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:37956/XMLDataPost");
                httpWebRequest.ContentType = "application/xml";
                httpWebRequest.Method = "POST";
    
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string xml = "<Service.InquiryData xmlns=\"http://schemas.datacontract.org/2004/07/WcfRestApplication\">" +
                                      "<Email>zhang@123.com</Email>" +
                                      "<FirstName>jhon</FirstName>" +
                                      "<Interest>eat</Interest>" +
                                      "<LastName>green</LastName>" +
                                      "<PhoneNumber>123456789</PhoneNumber>" +
                                "</Service.InquiryData>";
    
                    streamWriter.Write(xml);
                    streamWriter.Flush();
                    streamWriter.Close();
    
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        Response.Write(result);
                    }

    Similar issue, please follow below.

    http://stackoverflow.com/questions/19906692/how-to-send-xml-via-post-with-wcf

    http://stackoverflow.com/questions/11176740/post-request-to-the-wcf-service-by-the-use-of-httpwebrequest

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 3, 2014 10:46 PM

All replies

  • User-417640953 posted

    Hi prescottvall,

    Thank you post the issue to asp.net forum.

    According to your description and code provided, I see you want to post xml data to wcf rest service.

    As for you just pass a single parameter "InquiryData" to rest method. So I suggest you set the BodyStyle as WebMessageBodyStyle.bare.

    And the Method should be "Post".

    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "/XMLDataPost", BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    public void XMLDataPost(InquiryData inquerydata);
           

    Then you can post xml to it like below.

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:37956/XMLDataPost");
                httpWebRequest.ContentType = "application/xml";
                httpWebRequest.Method = "POST";
    
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string xml = "<Service.InquiryData xmlns=\"http://schemas.datacontract.org/2004/07/WcfRestApplication\">" +
                                      "<Email>zhang@123.com</Email>" +
                                      "<FirstName>jhon</FirstName>" +
                                      "<Interest>eat</Interest>" +
                                      "<LastName>green</LastName>" +
                                      "<PhoneNumber>123456789</PhoneNumber>" +
                                "</Service.InquiryData>";
    
                    streamWriter.Write(xml);
                    streamWriter.Flush();
                    streamWriter.Close();
    
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        Response.Write(result);
                    }

    Similar issue, please follow below.

    http://stackoverflow.com/questions/19906692/how-to-send-xml-via-post-with-wcf

    http://stackoverflow.com/questions/11176740/post-request-to-the-wcf-service-by-the-use-of-httpwebrequest

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 3, 2014 10:46 PM
  • User-250408672 posted

    Why is the public listed as a prohibited modifier in Resharper. Other than that it looks pretty straight forward

    public void XMLDataPost(InquiryData inquerydata);
    Thursday, June 5, 2014 2:56 PM
  • User-417640953 posted

    public void XMLDataPost(InquiryData inquerydata);

    Hi,

    The public keyword is optional, have you try my code provided in previous post.

    If there any doubt, please feel free to back. Thanks.

    Regards!

    Thursday, June 5, 2014 10:58 PM