Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

wininet, http post to Apache servers and request headers

291 views
Skip to first unread message

Colin Hughes

unread,
Jun 4, 2002, 4:39:28 AM6/4/02
to
I am receiving variations of:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><HTML><HEAD> <TITLE>400
Bad Request</TITLE></HEAD><BODY> <H1>Bad Request</H1> Your browser sent a
request that this server could not understand.<P> Request header field is
missing colon separator.<P> <PRE> TSkillSite� </PRE> <P> <HR>
<ADDRESS>Apache/1.3.12 Server at gibberish.co.uk Port 80</ADDRESS>
</BODY></HTML>

when I make a 'post' via wininet to several Apache servers.

I use this line to add my header:
HttpAddRequestHeaders(hHttpOpenRequest, PChar(sHeader),
Length(sHeader),
HTTP_ADDREQ_FLAG_REPLACE or HTTP_ADDREQ_FLAG_ADD);

The header is sHeader = 'Content-Type:
application/x-www-form-urlencoded'#13#10;

I have tried not *adding* a request header at all (ie not calling
HttpAddRequestHeaders): This then fails posting to all http servers.

I have tried the following variations of sHeader thus:
'Content-Type:application/x-www-form-urlencoded'#13#10#13#10;
'Content-Type:application/x-www-form-urlencoded';
'Content-Type:application/x-www-form-urlencoded\r\n';
'Content-Type:application/x-www-form-urlencoded\n\n';

I have tried to find documentation with example of HTTP request headers,
with no success

Please can anyone help me with more information or where I am going wrong,
or point me in the direction of more newgroups that may be able to help? I
have posted to the following borland.public.delphi groups with this post:
internet.websnap
intenet.winsock
objectpascal (a bit of topic, sorry: desperate man at large!)
winapi

Thanks in advance,

Colin Hughes.


Sven Bobrowski

unread,
Jun 4, 2002, 4:51:01 AM6/4/02
to
Hi,

here is an code example from MSDN.

I think they only add's an #13#10 on the end of the string, for Example
'Example'#13#10.

Here is the code (sorry, only VB, it Microsoft :) ):

<snip>
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Name : btSend_Click
' Purpose : Click event for button
' Parameters : NA
' Return val : NA
' Algorithm : Sends request to server
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub btSend_Click()
Dim iRetVal As Integer
Dim sBuffer As String * 1024
Dim lBufferLen As Long
Dim vDllVersion As tWinInetDLLVersion
Dim sStatus As String
Dim sOptionBuffer As String
Dim lOptionBufferLen As Long
Dim SecFlag As Long
Dim dwSecFlag As Long
Dim dwPort As Long


Screen.MousePointer = vbHourglass
btSend.Enabled = True
lBufferLen = Len(sBuffer)
If CBool(hInternetSession) Then
SetStatus "InternetQueryOption"
InternetQueryOption hInternetSession, INTERNET_OPTION_VERSION,
vDllVersion, Len(vDllVersion)
lblMajor = vDllVersion.lMajorVersion
lblMinor = vDllVersion.lMinorVersion
SetStatus "InternetConnect"
If checkSecure.Value = 1 Then
Debug.Print "Establishing secure connection" & " "
dwPort = INTERNET_DEFAULT_HTTPS_PORT
Debug.Print "Setting security flags" & " "
SecFlag = INTERNET_FLAG_SECURE Or _
INTERNET_FLAG_IGNORE_CERT_CN_INVALID Or _
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
Else
dwPort = INTERNET_DEFAULT_HTTP_PORT
SecFlag = 0
End If
hInternetConnect = InternetConnect(hInternetSession, CheckUrl,
dwPort, _
txtUsername.Text, txtPassword.Text,
INTERNET_SERVICE_HTTP, 0, 0)
'hInternetConnect = InternetConnect(hInternetSession, CheckUrl,
dwPort, _
vbNullString, vbNullString,
INTERNET_SERVICE_HTTP, 0, 0)
If hInternetConnect > 0 Then

SetStatus "HttpOpenRequest"

If optGet.Value = True Then
sOptionBuffer = vbNullString
lOptionBufferLen = 0
hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET",
GetUrlObject, "HTTP/1.0", vbNullString, 0, _
INTERNET_FLAG_RELOAD Or INTERNET_FLAG_KEEP_CONNECTION
Or SecFlag, 0)
Else
sOptionBuffer =
"MOVENEXT=MoveNext?CONTRACT=-1&ZIP=94025?STATE=CA"
lOptionBufferLen = Len(sOptionBuffer)
hHttpOpenRequest = HttpOpenRequest(hInternetConnect,
"POST", GetUrlObject, "HTTP/1.0", vbNullString, 0, _
INTERNET_FLAG_RELOAD Or SecFlag, 0)
End If

If CBool(hHttpOpenRequest) Then
SetStatus "HttpSendRequest"
Debug.Print sOptionBuffer
Dim sHeader As String

'sHeader = "Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd." & vbCrLf
'iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
'Debug.Print iRetVal & " " & Len(sHeader)

sHeader = "Accept-Language: en" & vbCrLf
iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
Debug.Print iRetVal & " " & Len(sHeader)

sHeader = "Connection: Keep-Alive" & vbCrLf
iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
Debug.Print iRetVal & " " & Len(sHeader);

'sHeader = "Content-Type: text/html" & vbCrLf ' "Accept =
image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd." &
vbCrLf
'iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
'Debug.Print iRetVal & " " & Len(sHeader)

Dim dwTimeOut As Long
dwTimeOut = 420000 ' time out is set to 7 minutes
iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_CONNECT_TIMEOUT, _
dwTimeOut, 4)
Debug.Print iRetVal & " " & Err.LastDllError & " " &
"INTERNET_OPTION_CONNECT_TIMEOUT"
iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_RECEIVE_TIMEOUT, _
dwTimeOut, 4)
Debug.Print iRetVal & " " & "INTERNET_OPTION_RECEIVE_TIMEOUT"
iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_SEND_TIMEOUT, _
dwTimeOut, 4)
Debug.Print iRetVal & " " & "INTERNET_OPTION_SEND_TIMEOUT"

Resend:
iRetVal = HttpSendRequest(hHttpOpenRequest, vbNullString,
0, sOptionBuffer, lOptionBufferLen)
If (iRetVal <> 1) And (Err.LastDllError = 12045) Then
MsgBox "Invalid CA"
'Certificate Authority is invalid.
Debug.Print "Invalid Cert Auth, resending" & " "
dwSecFlag = SECURITY_FLAG_IGNORE_UNKNOWN_CA
iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_SECURITY_FLAGS, dwSecFlag, 4)
Debug.Print iRetVal & " " & Err.LastDllError & " " &
"INTERNET_OPTION_SECURITY_FLAGS"
GoTo Resend
End If

If iRetVal Then
Dim dwStatus As Long, dwStatusSize As Long
dwStatusSize = Len(dwStatus)
HttpQueryInfo hHttpOpenRequest, HTTP_QUERY_FLAG_NUMBER
Or HTTP_QUERY_STATUS_CODE, dwStatus, dwStatusSize, 0
Select Case dwStatus
Case HTTP_STATUS_PROXY_AUTH_REQ
iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_PROXY_USERNAME, _
"IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_PROXY_PASSWORD, _
"IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
GoTo Resend
Case HTTP_STATUS_DENIED
iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_USERNAME, _
"IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_PASSWORD, _
"IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
GoTo Resend
End Select

SetStatus "HttpQueryInfo"
'response headers
GetQueryInfo hHttpOpenRequest, lblContentType,
HTTP_QUERY_CONTENT_TYPE
GetQueryInfo hHttpOpenRequest, lblContentLength,
HTTP_QUERY_CONTENT_LENGTH
GetQueryInfo hHttpOpenRequest, lblLastModified,
HTTP_QUERY_LAST_MODIFIED
GetQueryInfo hHttpOpenRequest, lblVersion,
HTTP_QUERY_VERSION
GetQueryInfo hHttpOpenRequest, lblStatusCode,
HTTP_QUERY_STATUS_CODE
GetQueryInfo hHttpOpenRequest, lblStatusText,
HTTP_QUERY_STATUS_TEXT
GetQueryInfo hHttpOpenRequest, lblRawHeaders,
HTTP_QUERY_RAW_HEADERS
GetQueryInfo hHttpOpenRequest, txtResponseHeaders,
HTTP_QUERY_RAW_HEADERS_CRLF
GetQueryInfo hHttpOpenRequest, lblForwarded,
HTTP_QUERY_FORWARDED
GetQueryInfo hHttpOpenRequest, lblServer, HTTP_QUERY_SERVER
GetQueryInfo hHttpOpenRequest, lblRequestMethod,
HTTP_QUERY_REQUEST_METHOD
GetQueryInfo hHttpOpenRequest, lblPragma,
HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_PRAGMA
GetQueryInfo hHttpOpenRequest, txtRequestHeaders,
HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_RAW_HEADERS_CRLF
GetQueryInfo hHttpOpenRequest, lblUserAgent,
HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_USER_AGENT
GetQueryInfo hHttpOpenRequest, lblRequestMethod2,
HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_REQUEST_METHOD
sStatus = "Ready"
btSend.Enabled = False
btGet.Enabled = True
Else
' HttpSendRequest failed
sStatus = "HttpSendRequest call failed; Error code: " &
Err.LastDllError & "."
End If
Else
' HttpOpenRequest failed
sStatus = "HttpOpenRequest call failed; Error code: " &
Err.LastDllError & "."
End If
Else
' InternetConnect failed
sStatus = "InternetConnect call failed; Error code: " &
Err.LastDllError & "."
End If
Else
' hInternetSession handle not allocated
sStatus = "InternetOpen call failed: Error code: " &
Err.LastDllError & "."
End If
SetStatus sStatus
Screen.MousePointer = vbDefault
End Sub
</snip>

Bobo

Colin Hughes wrote:
> I am receiving variations of:
>
> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><HTML><HEAD> <TITLE>400
> Bad Request</TITLE></HEAD><BODY> <H1>Bad Request</H1> Your browser sent a
> request that this server could not understand.<P> Request header field is

> missing colon separator.<P> <PRE> TSkillSite?8 ¼ </PRE> <P> <HR>

Colin Hughes

unread,
Jun 4, 2002, 6:45:24 AM6/4/02
to
Bobo,

That is my orignal attempt:


> > The header is sHeader = 'Content-Type:
> > application/x-www-form-urlencoded'#13#10;

Thanks anyway.

Colin.

"Sven Bobrowski" <Sven.Bo...@EasternGraphics.com> wrote in message
news:3CFC7F75...@EasternGraphics.com...

Colin Hughes

unread,
Jun 4, 2002, 6:44:58 AM6/4/02
to
Bobo,

Perhaps the wording of my post was not very clear, but that is what I am
doing and Apache servers return a bad request.

> > The header is sHeader = 'Content-Type:
> > application/x-www-form-urlencoded'#13#10;

Thanks anyway,

Colin.

"Sven Bobrowski" <Sven.Bo...@EasternGraphics.com> wrote in message
news:3CFC7F75...@EasternGraphics.com...

Rich Bouchard

unread,
Jun 9, 2002, 5:06:05 PM6/9/02
to
Colin,

Here's a simple attempt at a solution...(well, it worked for me after 2 days
of pulling my hair out!)

Try using 'POST' as the method as opposed to 'post' or 'Post'.

Good luck.

richb

"Colin Hughes" <chu...@maddisoncurwood.com> wrote in message
news:adi5o3$rkv$1...@paris.btinternet.com...

Colin Hughes

unread,
Jun 12, 2002, 6:01:08 AM6/12/02
to
My problem was that I was posting to a page that then posts to another page.
As soon as I posted direct to the ultimate page it worked.


"Colin Hughes" <chu...@maddisoncurwood.com> wrote in message

news:3cfc7dd9_1@dnews...

0 new messages