Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/chkUserPwd", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        void CheckUserAndPassword(string user, string pwd);
    }

C#
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   public class Service1 : IService1
   {

       public void CheckUserAndPassword(string user, string pwd)
       {
           if (user == "me" && pwd == "123")
           {
               //return true;
               HttpContext.Current.Response.Write("Welcome");
           }
           else
           {
               //return false;
               HttpContext.Current.Response.Write("Invalid");
           }
       }
   }


--Web.Config--
XML
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="BehaveWebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>        
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>        
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="BizConnectPro_Webservice.Service1">
        <endpoint behaviorConfiguration="BehaveWebHttp" binding="webHttpBinding"
          bindingConfiguration="" contract="BizConnectPro_Webservice.IService1" />
      </service>
    </services>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>


I am using below code to use this web service through HttpWebRequest and
got error "The remote server returned an error: (400) Bad Request." It working fine for get method.----

C#
string postData = string.Format("user={0}&pwd={1}", "me", "123");
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:52631/Service1.svc/chkUserPwd");
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postData.Length;
            try
            {
                using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
                {
                    requestWriter2.Write(postData);
                }

                using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    // dumps the HTML from the response into a string variable
                    postData = responseReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
Posted
Updated 31-May-13 19:26pm
v2
Comments
Sergey Alexandrovich Kryukov 31-May-13 20:09pm    
In can be difficult, because WCF has a number of internal machinery: service contracts, data contracts, etc. You will feel really blind-folded in parsing the messages. Why not acting as a normal WCF client?
If you say that you don't have the interfaces, I'll answer that handling generation of HTTP requests and parsing HTTP responses on the HttpWebRequest level won't make it easier...
—SA
rajesh@1989 31-May-13 20:33pm    
I want url based service without using wcf client just like asmx service.
Sergey Alexandrovich Kryukov 1-Jun-13 21:40pm    
If you really want it, make it, who stops you? Perhaps wanting is not enough?
—SA
rajesh@1989 1-Jun-13 2:53am    
I got the solution...
Sergey Alexandrovich Kryukov 1-Jun-13 21:40pm    
Well...

1 solution

C#
credential obj = new credential();
            obj.user = "me";
            obj.pwd = "123";
            JavaScriptSerializer js = new JavaScriptSerializer();
            string postData= js.Serialize(obj);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:52631/Service1.svc/chkUserPwd");
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json"
            webRequest.ContentLength = postData.Length;
            try
            {
                using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
                {
                    requestWriter2.Write(postData);
                }

                using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    // dumps the HTML from the response into a string variable
                    postData = responseReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

C#
public class credential
        {
            public string user { get; set; }
            public string pwd { get; set; }
        }

As I mentioned on WebInvoke is BodyStyle = WebMessageBodyStyle.Wrapped then we must post the data json or xml.
 
Share this answer
 
Comments
EddieCluverius 18-Dec-14 14:35pm    
I still get the bad request error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900