-
November 21st, 2011, 01:50 PM
#1
Calling web services
I am trying to call a web service written in C# net using Wininet. The web service has been hosted in IIS. The web service url is /Trial/Service1.asmx and it has only the default HelloWorld method in it. I can access the web service via the browser by the url http://localhost/Trial/Service1.asmx . I can call the web service method HelloWorld using the Invoke button on the browser which we get on giving the url. But the app returns 400 ( Bad request ). If I use GET instead of POST I am getting 200 OK. But I need to use the POST method.
What could be wrong?
Here is the code I am using
//m_lpOutBuffer is a DWORD data member and initialized with 0.
Code:
{
CString csSoapString;
csSoapString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
csSoapString += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
csSoapString += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema \"";
csSoapString += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
csSoapString += "<soap:Body>";
csSoapString += "<HelloWorld xmlns:m=\"http://tempuri.org/\"/>";
csSoapString += "</soap:Body>";
csSoapString += "</soap:Envelope>";
LPTSTR AcceptTypes[2] = {TEXT("text/xml"), NULL};
DWORD dwFlags = INTERNET_FLAG_NO_CACHE_WRITE |INTERNET_FLAG_KEEP_CONNECTION;
HINTERNET hNet = InternetOpen(L"", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0 );
if (hNet)
{
HINTERNET hSession = InternetConnect(hNet, _T("localhost"),
INTERNET_DEFAULT_HTTP_PORT,
_T(""),
_T(""), INTERNET_SERVICE_HTTP, 0, 0);
if (hSession)
{
hRequest = HttpOpenRequest(hSession, _T("POST"),
_T("/Trial/Service1.asmx"),
_T("HTTP/1.1"),NULL,
(LPCTSTR*)AcceptTypes, dwFlags, 0);
if (hRequest)
{
int nContentLength = csSoapString.GetLength();
CString csLength;
csLength.Format( _T( "%d" ),nContentLength );
CString csSoapAction;
csSoapAction += _T("Content-Type: text/xml;charset=utf-8\r\n");
csSoapAction += _T( "Content-Length: ");
csSoapAction += csLength;
csSoapAction += _T("SOAPAction: \"" );
csSoapAction += _T("http://tempuri.org/HelloWorld");
csSoapAction += _T("\"\0\r\n\r\n");
BOOL bSent = HttpSendRequest(hRequest, ( LPCTSTR )csSoapAction, csSoapAction.GetLength(), (LPVOID )(LPCTSTR)csSoapString, csSoapString.GetLength());
DWORD dwBufferSize = 0;
m_lpOutBuffer = 0;
// read the reponse
ReadQueryResponse ( HTTP_QUERY_RAW_HEADERS_CRLF, dwBufferSize );
TCHAR* pLBuffer = (TCHAR* )m_lpOutBuffer;
CString csStatusCode( pLBuffer, dwBufferSize + 1 );
DWORD dwRetVal = _wtol((TCHAR*)(LPCTSTR)csStatusCode);
int nReponseSize = _tcslen( pLBuffer );
if( 0 == nReponseSize )
{
return;
}
CString csResposeHeader( pLBuffer, nReponseSize );
OutputDebugString( csResposeHeader ); // shows 400( bad request )
}
}
}
}
bool CWinInet_ClientDlg::ReadQueryResponse( UINT uQueryLevel, DWORD& dwBufferSize )
{
try
{
if( !HttpQueryInfo( hRequest, uQueryLevel,(LPVOID)m_lpOutBuffer, &dwBufferSize, 0 ))
{
DWORD dwErrorCode = GetLastError();
// If buffer size is not sufficient, call QueryInfo() with the new buffer size.
if( ERROR_INSUFFICIENT_BUFFER == dwErrorCode )
{
if( 0 != m_lpOutBuffer )
{
delete[] m_lpOutBuffer;
m_lpOutBuffer = 0;
}
m_lpOutBuffer = new char[dwBufferSize];
if( 0 == m_lpOutBuffer )
{
return false;
}
return ReadQueryResponse( uQueryLevel,dwBufferSize );
}
else if( dwErrorCode == ERROR_HTTP_HEADER_NOT_FOUND )
{
return false;
}
}
return true;
}
catch( ... )
{
return false;
}
}
Thanks in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|