none
Passing xml string to REST WCF service with additional parameters RRS feed

  • Question

  • I have a WCF service method that accepts 3 parameters that are all strings and will return back some data.

     [OperationContract]
           [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
          RequestFormat = WebMessageFormat.Xml, UriTemplate = "MyHttpRequest/{request}/{LogonID}/{Key}")]
           string TestRequest(string request, string LogonID, string Key);

    The request string parameter is supposed to be xml. Here is an example of the input, there are additional attributes but here is a sample.

    <DateOfBirth>19000101</DateOfBirth>

    I am calling this code from client like this

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://someurl.com/LookupService.svc/MyHttpRequest/" + this.tbRequestString.Text.Trim() + "/" + this.tbLoginID.Text.Trim() + "/" + this.tbKey.Text.Trim());

     req.Method = WebRequestMethods.Http.Get;

                    string result = null;
                    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                    {
                        StreamReader reader = new StreamReader(resp.GetResponseStream());
                        result = reader.ReadToEnd();
                    }

    When I attempt to construct the URI I get a "A potentially dangerous Request.Path value was detected from the client (<). " 400 Bad Request. What am I doing wrong? I cant see a way to do it, and articles on the web dont seem to help or are related to POST not GET. Any ideas?




    • Edited by spark29er Wednesday, July 29, 2015 6:27 PM
    Wednesday, July 29, 2015 5:41 PM

Answers

  • Hi spark29er,

    According to this issue.I suggest you can set "ContentType " to your client code.And I write a demo

    to test it . If the client didn't set "ContentType ", it will prompt an error is that 400 Bad Request.

    My client code shown below:

    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {           
                try
                {
                    string strPost = "hello here is my test app" + "By doing this, we can try to cater"                 
                    byte[] buffer = Encoding.UTF8.GetBytes(strPost);
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://LocalHost:7789/MySericeDemo/postMostStr/" + buffer.Length);
                    request.Method = "POST";
                    request.ContentType = "text/plain";
                    request.ContentLength = buffer.Length;
                    System.IO.Stream requestStream = request.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Close();
                    System.IO.Stream getStream = request.GetResponse().GetResponseStream();
                    byte[] resultByte = new byte[200];
                    getStream.Read(resultByte, 0, resultByte.Length);
                    Console.WriteLine(Encoding.UTF8.GetString(resultByte));
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                Console.ReadLine();
            }
        }
    }

    My service code shown below:

    public string postMostStr(string streamlength, Stream getStream)
            {
                try
                {
                    int bylength = Convert.ToInt32(streamlength);
                    byte[] resultByte = new byte[bylength];
                    getStream.Read(resultByte, 0, resultByte.Length);
                    Console.WriteLine(Encoding.UTF8.GetString(resultByte));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                Console.WriteLine("success!");
                return "success";
            }

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj



    Best Regards, Tracy Dj

    Thursday, July 30, 2015 5:53 AM
    Moderator