Sending a string from vb to java servlet

S

Swati

Hi, I am trying to send a string from VB to a java servlet. I am using
wininet dll on the vb side. VB code is as follows:

Public Const scUserAgent = "my wininet app"
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const HTTP_ADDREQ_FLAG_ADD_IF_NEW = &H10000000
Public Const HTTP_ADDREQ_FLAG_ADD = &H20000000
Public Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000
Public Const INTERNET_DEFAULT_HTTP_PORT = 80
Public Const INTERNET_SERVICE_HTTP = 3
Public Const INTERNET_FLAG_RELOAD = &H80000000

Public Declare Function InternetOpen Lib "wininet.dll" Alias _
"InternetOpenA" (ByVal sAgent As String, ByVal lAccessType _
As Long, ByVal sProxyName As String, ByVal sProxyBypass As _
String, ByVal lFlags As Long) As Long

Public Declare Function InternetConnect Lib "wininet.dll" Alias _
"InternetConnectA" (ByVal InternetSession As Long, _
ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, _
ByVal lService As Long, ByVal lFlags As Long, _
ByVal lContext As Long) As Long

Public Declare Function HttpOpenRequest Lib "wininet.dll" Alias _
"HttpOpenRequestA" (ByVal hHttpSession As Long, ByVal sVerb As _
String, ByVal sObjectName As String, ByVal sVersion As String, _
ByVal sReferer As String, ByVal something As Long, ByVal lFlags
_
As Long, ByVal lContext As Long) As Long

Public Declare Function HttpSendRequest Lib "wininet.dll" Alias _
"HttpSendRequestW" (ByVal hHttpRequest As Long, ByVal sHeaders _
As String, ByVal lHeadersLength As Long, sOptional As Any, _
ByVal lOptionalLength As Long) As Integer

Public Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer

Sub test()
Dim hInternetSession As Long
Dim hInternetConnect As Long
Dim hHttpOpenRequest As Long

Dim ret As Boolean


hInternetSession = InternetOpen(scUserAgent, _
INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString,
0)
hInternetConnect = InternetConnect(hInternetSession, _
"localhost", INTERNET_DEFAULT_HTTP_PORT, _
vbNullString, vbNullString, INTERNET_SERVICE_HTTP, 0, 0)
hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "POST", _
"/web/MyServlet?Type=send", "HTTP/1.0", vbNullString, 0,
_
INTERNET_FLAG_RELOAD, 0)

If hHttpOpenRequest <> 0 Then
Dim sHeader As String
Const HTTP_ADDREQ_FLAG_ADD = &H20000000
Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000
sHeader = "Content-Type: application/x-www-form-urlencoded" _
& vbCrLf
bRet = HttpAddRequestHeaders(hHttpOpenRequest, _
sHeader, Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE _
Or HTTP_ADDREQ_FLAG_ADD)

ret = HttpSendRequest(hHttpOpenRequest, header, Len(header),
"hello", Len("hello"))

InternetCloseHandle (hHttpOpenRequest)

End If



InternetCloseHandle (hInternetConnect)
InternetCloseHandle (hInternetSession)

End Sub


MyServlet.java's code is as follows:


import java.util.HashMap;
import java.util.Properties;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;



/**
* DOCUMENT ME!
*
* @author
* @version 1.0
*/
public class MyServlet extends HttpServlet
{
public synchronized void init(ServletConfig config) throws
ServletException
{
super.init(config);
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
}

/**
* Process the HTTP Post request
*/
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
String strType = (String)request.getParameter("Type");
System.out.println("here");
System.out.println("characterEncoding = " +
request.getCharacterEncoding());
Enumeration enums = request.getHeaderNames();
while (enums.hasMoreElements())
{
String header = (String)enums.nextElement();
System.out.println(header + ":" + request.getHeader(header));
}
System.out.println("contentType = " +
request.getContentType());

try
{
InputStream in = request.getInputStream();
if (in != null)
{
String fileLocation = "D:\\string.dat";
File f = new File(fileLocation);
FileOutputStream out = new FileOutputStream(f);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] buf = new byte[1024*10];
int n;
while ((n = bis.read(buf)) >= 0)
{
System.out.println("bytes read = " + n);
out.write(buf, 0, n);
}
out.flush();
out.close();

}
}catch(Exception e) {e.printStackTrace();}
}

}



The string.dat file contains only junk characters. Appreciate if
anybody can help solve this problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top