WCF REST POST error bad request 400

Posted by lyatcomit on Stack Overflow See other posts from Stack Overflow or by lyatcomit
Published on 2010-04-21T09:57:18Z Indexed on 2010/04/21 10:03 UTC
Read the original article Hit count: 1022

Filed under:
|
|

Here's my code:

DOAMIN:

using System;
using System.Collections;
using System.Runtime.Serialization;

namespace Comit.TrafficService.Services.Mobile
{
    [DataContract(Namespace = "http://192.168.0.161:9999/TrafficService/Domain/Mobile")]
    public class Error
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public DateTime Time { get; set; }

        public string Message { get; set; }

        [DataMember]
        public string Stacktrace { get; set; }

        [DataMember]
        public string Os { get; set; }

        [DataMember]
        public string Resolution { get; set; }
    }
}

CONTRACT:

using System.ServiceModel;
using System.ServiceModel.Web;
using Comit.TrafficService.Services.Mobile;

namespace Comit.TrafficService.Services.Contracts
{
    [ServiceContract(Name = "MobileErrorService")]
    public interface IMobileError
    {
        /// <summary>
        /// ???????????
        /// </summary>
        /// <param name="Error">??????</param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedResponse, 
            UriTemplate = "ErrorReport",
            RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml)
        ]
        int ErrorReport(Error error);
    }
}

SERVICE:

using System.ServiceModel.Web;
using Comit.TrafficService.Services.Contracts;
using Comit.TrafficService.Dao.Mobile;
using System;
using Comit.TrafficService.Services.Mobile;

namespace Comit.TrafficService.Services
{
    public class MobileErrorService : IMobileError
    {
        public int ErrorReport(Error error)
        {
            return HandleAdd(error);
        }

        public int HandleAdd(Error error)
        {
            Console.WriteLine("?????error.Message:" + error.Message);
            ErrorDao edao = new ErrorDao();
            Console.WriteLine("??error" );
            int result = (int)edao.Add(error);

            return result;
        }
    }
}

Configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Comit.TrafficService.Services.MobileErrorService">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.0.161:9999"/>
          </baseAddresses>
        </host>
        <endpoint address="http://192.168.0.161:9999/Comit/TrafficService/Services" binding="webHttpBinding" contract="Comit.TrafficService.Services.Contracts.IMobileError"
        behaviorConfiguration="RestfulBehavior" name="webHttpBinding">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
          <behavior name="RestfulBehavior">
            <webHttp/>
            <dataContractSerializer ignoreExtensionDataObject="true"/>
          </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Host:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Comit.TrafficService.Services;

namespace ServiceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(MobileErrorService)))
            {
                host.Opened += delegate
                {
                    Console.WriteLine("CalculaorService????,????????!");
                };
                host.Open();
                Console.Read();
            }  
        }
    }
}

Client code:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using TestWCFRest.WcfServices.Services;
using System.Net;

namespace TestWCFRest.WcfServices.Hosting
{
    class Program
    {
         static void Main(string[] args)  
         {
             //using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
             //{
             //    host.Opened += delegate
             //    {
             //        Console.WriteLine("CalculaorService????,????????!");
             //    };
             //    host.Open();
             //    Console.Read();
             //}
             HttpWebRequest req = null;
             HttpWebResponse res = null;
             try
             {
                 string url = "http://192.168.0.161:9999/Comit/TrafficService/Services/ErrorReport";
                 req = (HttpWebRequest)WebRequest.Create(url);
                 req.Method = "POST";
                 req.ContentType = "application/xml; charset=utf-8";
                 req.Timeout = 30000;
                 req.Headers.Add("SOAPAction", url);

                 System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                 xmlDoc.XmlResolver = null;
                 xmlDoc.Load(@"d:\test.xml");
                 string sXML = xmlDoc.InnerXml;
                 req.ContentLength = sXML.Length;
                 System.IO.StreamWriter sw = new System.IO.StreamWriter(req.GetRequestStream());
                 sw.Write(sXML);
                 sw.Close();

                 res = (HttpWebResponse)req.GetResponse();
             }
             catch (Exception ex)
             {
                 System.Console.WriteLine(ex.Message);
             } 

         }
    }
}

It's my first time I'm trying to do somethinf with WCF so I don't know how to solve this problem. Since there is a lot of professionals here, I would appreciate your help in solving this. Thank you in advance!

© Stack Overflow or respective owner

Related posts about post

Related posts about wcf