none
How do I configure IIS 7.5 to handle body from Apache with WCF REST service RRS feed

  • Question

  • I have a REST service in IIS 7.5 that is working properly with my calls, but when Apache sends a request, the body is discarded and the length is set to 0.

    Web config:

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
     
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </modules>
      </system.webServer>
     
      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
        <standardEndpoints>
          <webHttpEndpoint>
            <!-- 
                Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
                via the attributes on the <standardEndpoint> element below
            -->
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>
    

    Service Description:

       

        [ServiceContract]     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]     publicclassMedherentRESTservice     {         #region Register User         [Description("Returns ACK msg using requested database: Staging, Demo or Production")]         [WebInvoke(UriTemplate = "RegisterUser", Method = "POST")]         publicstring RegisterUser(string hl7StringIn)

    I am expecting the REST body to come in in the hl7StringIn variable and it does when I use the following call:

                 
    using (HttpClient proxy = newHttpClient("http://wwwstaging.carasolva.com/MedherentWCFRest/"))
    
                proxy.DefaultHeaders.Add("login_id""t");
                proxy.DefaultHeaders.Add("passcode""p");
                proxy.DefaultHeaders.Add("hl7TransactionHeader"Constants.MhRegisterUserRequestHeader);
      
                 var uri = "RegisterUser";
                var c = Constants.MhRegisterUserRequestBody;
                var content = HttpContentExtensions.CreateDataContract(c);
    
                try
                {
                    using (HttpResponseMessage response = proxy.Post(uri, content))
                    {
                        response.EnsureStatusIsSuccessful();
                        var responseText = response.Content.ReadAsString();
                        Console.WriteLine(responseText);
                    }
    

    WireShark shows me that a POST is made that includes only the header, with a continuation flag set. IIS sends the ACK and the continuation is sent (also as a POST), merged with the headers and passed into my service.

    Here is the code on the Apache side:

    HttpClient httpclient =  new DefaultHttpClient();     
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = "";
    HttpPost httppost = new HttpPost(request.getHost());                        
    for(@SuppressWarnings("rawtypes") Header header : request.getHeaders()){
        httppost.setHeader(header.getKey(), header.getValue());
    }
    if(requestBody.length()>0){
          // append the body data to the post
          StringBuilder sb = new StringBuilder();
          sb.append(requestBody);
          StringEntity stringentity = new StringEntity(sb.toString(), HTTP.UTF_8);
          httppost.setEntity(stringentity);                             
    }
    // hit the server
    response = httpclient.execute(httppost, responseHandler);
    // clean up 
    httppost = null;
    return response;    

    For completeness, the block:

    if(requestBody.length()>0){
          // append the body data to the post
          StringBuilder sb = new StringBuilder();
          sb.append(requestBody);
          StringEntity stringentity = new StringEntity(sb.toString(), HTTP.UTF_8);
          httppost.setEntity(stringentity);                             
    }

    is getting executed.

    The code is using the Apache HTTP stack:

    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;

    import org.apache.http.protocol.HTTP;

    Wireshark is showing only one POST with a body length = 0;

    I hope this is enough information for someone to help.

    According to the developer using Apache, he can send his request to IIS 6 and the body is there.

    He has posted his side here:

    http://stackoverflow.com/questions/16820176/iis-7-5-handles-restful-post-differently-than-other-servers


    Scott Simpson







    Scott Simpson

    Monday, June 3, 2013 9:15 PM

Answers

  • Hi Scott,

    Based on my understanding, you've a WCF REST service setup in IIS SERVER and an apache java based client will send HTTP post request to access the REST service; and now you find that request sent from apache client always has empty body, correct?

    For WCF REST service, I haven't found any known issue which will strip the body/content of request. For the wireshake log, did you capture them at the apache client side or the WCF REST service server-side? For server-side, one thing you can check is if there is any IIS specific extensions that might intercept and modify the incoming requests. For client-side (on the apache java client machine), you can tried running a .NET based REST service client to see if the same behavior occurs. If a .NET based client works, the problem should be specific to apache java client. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 4, 2013 9:39 AM
    Moderator