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.
Scott Simpson