Patrick
Patrick

Reputation: 8318

400.0 - Bad Request with MSXML2::IXMLHTTPRequestPtr

I'm trying to access an xml file over http. The address is similar to:

http://localhost/app/config/file.xml

When pasting this in a browser the xml is displayed as expected. In my software I am using:

MSXML2::IXMLHTTPRequestPtr rp;
...
rp->open( "GET" , "http://localhost/app/config/file.xml" );

and getting the following response:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>IIS 7.0 Detailed Error - 400.0 - Bad Request</title> 
<style type="text/css"> 
<!

Does anyone have any ideas what might be happening?

EDIT: more info, when trying to connect to another server in the office (which I can connect to fine using a browser) I'm getting a response of "Bad Request" and no more.

The function making the call is:

#import "msxml3.dll"

BOOL HTTPGet(LPCTSTR URL, CString &Response, long Timeout, BOOL ForceNoCache )
{
BOOL ret = FALSE;

MSXML2::IXMLHTTPRequestPtr rp;
//MSXML2::IServerXMLHTTPRequestPtr rp;

try
{
    HRESULT hr = rp.CreateInstance( __uuidof( MSXML2::XMLHTTP ) );
    //HRESULT hr = rp.CreateInstance( __uuidof( MSXML2::ServerXMLHTTP30 ) );

    if( SUCCEEDED( hr ) )
    {
        rp->open( "GET" , URL );
        if( ForceNoCache )
            rp->setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT" );
        rp->send();

        for( DWORD tc = GetTickCount() ; rp->readyState != 4 && GetTickCount() < tc + Timeout ; )
        {
            Sleep( 50 );
            //WaitMessage();
            for( MSG msg ; PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ; )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }

        }

        if( rp->readyState == 4 )
        {
            Response = (LPCSTR)rp->responseText;
            ret = TRUE;
        }
        else
            Response = "Timed out";
    }
}
catch( CException &e )
{
    char err[ 2048 ];
    if( e.GetErrorMessage( err , sizeof( err ) ) )
        Response = err;
    else
        Response = "Unhandled exception";
}
catch( _com_error &e )
{
    Response = e.ErrorMessage();
}
catch( ... )
{
    Response = "Unhandled exception";
}

return ret;
}

EDIT2: The function works correctly when ForceNocache set to false making the If-Modified-Since header the problem... I'll start up a new question if I can't work out why this isn't liked (by iis 7) but if someone here can point me in the right direction it will save some noise.

Thanks for all your help.

Upvotes: 1

Views: 2308

Answers (2)

user215361
user215361

Reputation:

I think I found your answer, you have to prefix the date with 0.

XmlHttpRequest with If-Modified since, webserver returns "400 Bad Request"

Upvotes: 1

Kev
Kev

Reputation: 119856

According to this Microsoft knowledge base article you may be experiencing some kind of packet corruption:

Error Message: HTTP/1.1 Error 400 - Bad Request (KB247249)

Just out of interest, do you experience the same when using IServerXMLHTTPRequestPtr?

Upvotes: 1

Related Questions