Can I use wsHttpBinding in WCF Service for HTTP POST requests? RRS feed

  • Question

  • User-1353668322 posted

    I have a simple WCF service with custom username-password authentication. I have a Windows Forms application that uses this service and it works fine. I need to know how can I call this service through an ASP.Net or any HTML page with same wsHttpBinding using HTTP POST request? I tried webHttpBinding (as I thought it should work) but the authentication does not work and service can be accessed without user credentials, which is unacceptable. I can't get wsHttpBinding work in this case. I can use this service this way...

    ServiceClient proxy = new ServiceClient();
    proxy.ClientCredentials.UserName.UserName = "umais";
    proxy.ClientCredentials.UserName.Password = "asghar";
    proxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
    string reply = proxy.Hello("Umais");

    but I have to call it in another way, through HTTP POST request, for that purpose, I need to have something like this...

    string url = "http://localhost:56779/Service.svc/Hello";
    string parameters = "{'Name': 'Umais'}";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentLength = 0;
    req.ContentType = "application/soap+xml; charset=utf-8";
    if (!string.IsNullOrEmpty(parameters))
        byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
        req.ContentLength = byteArray.Length;
        Stream dataStream = req.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
    HttpWebResponse response = (HttpWebResponse)req.GetResponse();

    I'm unable to consume the service through HttpWebRequest, can anyone help me out here please? (basicHttpBinding is also unacceptable because of its lack of security)

    Thursday, January 17, 2013 6:53 AM


  • User-1662538993 posted

    HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
    req.Headers.Add("Password", "your password");
    req.Headers.Add("Username", "your username");
    req.Method = "POST";
    req.ContentLength = DATA.Length;
    req.KeepAlive = false;
    req.ContentType = "application/json";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2013 10:34 AM