[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URLLoader and HttpServletRequest with POST method

Posted on 2009-04-16
12
Medium Priority
?
3,158 Views
Last Modified: 2013-11-24
I have a problem with the variables of form submitted with POST.  In my doPost function of my servlet, I do not receive the variables, or at least they cannot be accessed in the HttpServletRequest variable.  Everything works fine with GET, but I'm sending a password, so I would rather use POST.

My Action Script code is included.  I'm using Tomcat and SSL as well.
var sendData:String = "command=logIn&user="+user+"&pw="+password;
var vars:URLVariables = new URLVariables(sendData);
var request:URLRequest = new URLRequest();
request.url = getBaseURL()+"ui";
request.method = URLRequestMethod.POST;
request.data = vars;
 
userLoader = new URLLoader();
userLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
userLoader.load(request);
 
userLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
userLoader.addEventListener(Event.COMPLETE, handleLogin);

Open in new window

0
Comment
Question by:palehose
  • 6
  • 6
12 Comments
 
LVL 12

Expert Comment

by:lexxwern
ID: 24155204
Try this:

var sendData:Object = new Object;
sendData.command = 'logIn';
sendData.user = user;
sendData.pw = password;
var vars:URLVariables = new URLVariables(sendData);
var request:URLRequest = new URLRequest();
request.url = getBaseURL()+"ui";
request.method = URLRequestMethod.POST;
request.data = vars;
userLoader = new URLLoader();
userLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
userLoader.load(request);
userLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
userLoader.addEventListener(Event.COMPLETE, handleLogin);

Open in new window

0
 

Author Comment

by:palehose
ID: 24155252
Thanks for the quick response, however, a couple of things:

1)The URLVariables constructor takes a String.
2)Regardless, the problem is not storing the data on the client, it's receiving it at the servlet.  I've checked to make sure that request has the correct data, and it does.  The servlet is not getting the variables.
0
 
LVL 12

Assisted Solution

by:lexxwern
lexxwern earned 1200 total points
ID: 24155276
Oh I am sorry... try this:
var sendData:Object = new Object;
sendData.command = 'logIn';
sendData.user = user;
sendData.pw = password;
var request:URLRequest = new URLRequest();
request.url = getBaseURL()+"ui";
request.method = URLRequestMethod.POST;
request.data = sendData;
userLoader = new URLLoader();
userLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
userLoader.load(request);
userLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
userLoader.addEventListener(Event.COMPLETE, handleLogin);

Open in new window

0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 

Author Comment

by:palehose
ID: 24155320
Again, this does the same thing I was already doing.  The problem is not storing the data, that's fine.  It has to be on the other end because the method works perfectly fine with GET.
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 24155346
Did you check if it worked or not?

Sending the data as aa URLVariable (i.e. euphemism for query string) will never work for the POST method so the way data is sent needs to be modified.
0
 

Author Comment

by:palehose
ID: 24155381
Sorry, I'm new to this, I did not know that.  I did try it and I'm getting the same results (nothing on the other end).  Is there another way to send the data?
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 24155409
Can you show me the JSP code for reading the POST variable as well?

Also, you should consider using HTTPServices in Flex. Sending POST data should be very easy thru that.

Here's a code sample:
<?xml version="1.0" encoding="utf-8"?>
 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns="*" creationComplete="send_data()">
 
    <mx:Script>
        <![CDATA[
            private function send_data():void {
                userRequest.send();
            }
        ]]>
    </mx:Script>
 
    <mx:Form x="22" y="10" width="493">
        <mx:HBox>
            <mx:Label text="Username"/>
            <mx:TextInput id="username"/>
        </mx:HBox>
        <mx:HBox>
            <mx:Label text="Email Address"/>
            <mx:TextInput id="emailaddress"/>
        </mx:HBox>
        <mx:Button label="Submit" click="send_data()"/>
    </mx:Form>
 
    <mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{userRequest.lastResult.users.user}">
        <mx:columns>
            <mx:DataGridColumn headerText="User ID" dataField="userid"/>
            <mx:DataGridColumn headerText="User Name" dataField="username"/>
        </mx:columns>
    </mx:DataGrid>
 
    <mx:TextInput x="22" y="292" id="selectedemailaddress"
        text="{dgUserRequest.selectedItem.emailaddress}"/>
 
    <mx:HTTPService id="userRequest" url="http://localhost/myproj/request_post2.php" useProxy="false" method="POST">
        <mx:request xmlns="">
            <username>{username.text}</username>
            <emailaddress>{emailaddress.text}</emailaddress>
        </mx:request>
    </mx:HTTPService>
 
</mx:Application>

Open in new window

0
 

Author Comment

by:palehose
ID: 24155525
Here's my doPost function.  As for using HTTPService, all I'm getting is a parse error when I try to use it.
public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
{
	try {
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection(url, "root", "");
		stmt = con.createStatement();
		
		PrintWriter out = response.getWriter();
		BufferedReader reader = request.getReader();
		
		Enumeration e = request.getHeaderNames();
		while(e.hasMoreElements()) {
			String s = (String) e.nextElement();
			System.out.println(s + ": " + request.getHeader(s));
		}
	
		String command = request.getParameter("command");
		//String user = request.getParameter("user");
 
		System.out.println("POST command: "+command);
		//System.out.println("user: "+user);
 
		if (command==null) {
		// No command
		response.setContentType("text/html");
		out.println("<body>");
		out.println("No command <p>");
		out.println("</body>");
		return;
		}
		
		if(command.equals("logIn")) {
			String username = request.getParameter("user");
			String password = request.getParameter("pw");
			String uid = "";
			
			System.out.println(username);
			System.out.println(password);
			
			String query = "SELECT * FROM users WHERE username='" + username + "'";
			ResultSet rs = stmt.executeQuery(query);
 
			while(rs.next()) {
				System.out.println(rs.getString("password"));
				if(password.equals(rs.getString("password"))) {
					uid = rs.getString("userid");
				}
			}
			
			out.println(uid);
			
		} 
		stmt.close();
		con.close();
	} catch(Exception e) {
		e.printStackTrace();
	}
}

Open in new window

0
 

Author Comment

by:palehose
ID: 24162281
So the header does include the Object.

From the header:
Referer: https://localhost:8443/app/app.swf
Content-type: application/x-www-form-urlencoded
Content-length: 15

[object Object]

I don't think it should be sent with that content type, so it sends the Object and not that string.  What content type should it be using, so I can just read the bytes?
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 24164896
Content type looks fine to me.

Did you include you form input valus as an object as the HTTPService.request object?
0
 

Accepted Solution

by:
palehose earned 0 total points
ID: 24164919
I ended up just sending some XML code and parsing it on the other end.  I think it has something to do with the Java, and I don't really have the time to figure it out, unfortunately.

Thanks for the help, I'll give you the points anyway.
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 24164943
Ok.. I understand your time constraints..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We've all had that page pop up telling us there is a problem with the certificate and some of us continue on anyways and others run away to a safer competing site.  But what to do when you get the error - is it your problem or theirs?  What can you …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question