Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Figuring out how to call a CGI script?  (Read 1833 times)
0 Members and 1 Guest are viewing this topic.
Offline bert

Junior Member




Miles of road and miles of code


« Posted 2010-04-19 02:49:46 »

Hello,
I'm currently working on an application as a side project, and I'm almost done... except for one thing. My program needs to call a CGI script by the POST method. Trouble is, I don't know what to send to the server. There are many possible parameters to be sent and I don't know what the server is expecting. I can get a generic response, something to the effect of "the server has no idea what you're trying to do", so I know that the program works, I just need to send the right data to the server.
Possible thoughts:
Is there a Firefox extension that would allow me to "capture" all traffic in and out of my computer? Or a Mac application that would do the same thing?

w00t, i'm winning
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2010-04-19 03:29:39 »

This should help: http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139

Offline bert

Junior Member




Miles of road and miles of code


« Reply #2 - Posted 2010-04-19 03:34:53 »

Thanks CaptainJester... but I can already connect to the server. Sorry for not being clear on that...
What I need is specific to the page I'm trying to get my program to interact with. Each page has its own way of handling data specific to its needs, and I want to figure out what the page is looking for. Is there a way of diving into the source of the page to get this?
To clarify, check out this page: http://www.webdeveloper.com/java/java_meets_cgi.html ... what I am looking for is what the server needs in the form_data field of this program.

w00t, i'm winning
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #3 - Posted 2010-04-19 09:43:27 »

Sounds like you want to know what an HTTP POST request looks like?

As for what exactly you need to send, that is up to the page. Since the code accepting the POST data is server side, there is no way for you to see its code. Use something like ProxyTrace or the FireFox plugin FireBug to see what is being posted when you access the page the normal way through your browser.

Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #4 - Posted 2010-04-19 10:06:44 »

If those Firefox extensions don't work out for you, WireShark will. It's not a lot of fun to use though.
Offline bert

Junior Member




Miles of road and miles of code


« Reply #5 - Posted 2010-04-19 20:17:22 »

Thank you so incredibly much for the FireBug tip. I was very easily able to view the raw HTTP request.

I pasted the request into my program and got the following response:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
HTTP/1.1 400 Bad Request
Date: Mon, 19 Apr 2010 18:00:00 GMT
Server: Apache/1.3.41 (Unix)
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!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>
POST /cgi-bin/multidb.cgi HTTP/1.0</PRE>
<P>
<HR>
<ADDRESS>Apache/1.3.41 Server at frwebgate5.access.gpo.gov Port 80</ADDRESS>
</BODY></HTML>


I literally copied it verbatim and added \r\n where necessary. How come it works for my browser but not for my program?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   protected void load() throws Exception {
      Socket sock = new Socket("frwebgate.access.gpo.gov", 80);
      String tempData;
      DataOutputStream sendData = new DataOutputStream(sock
            .getOutputStream());
      DataInputStream readData = new DataInputStream(sock
            .getInputStream());
      System.out.println("===========SENDING REQUEST==========");
      sendData.writeBytes("POST /cgi-bin/multidb.cgi HTTP/1.0\r\n");
      sendData.writeBytes("Content-Type: application/x-www-form-urlencoded\r\n");
      sendData.writeBytes("Content-Length: 1523\r\n");
      sendData.writeBytes("POST /cgi-bin/multidb.cgi HTTP/1.0\r\n");
      sendData.writeBytes("WAISdbName=2010_register+Federal+Register%2C+Volume+75+%282010%29&WAISdbName=2009_register+Federal+Register%2C+Volume+74+%282009%29&WAISdbName=2008_register+Federal+Register%2C+Volume+73+%282008%29&WAISdbName=2007_register+Federal+Register%2C+Volume+72+%282007%29&WAISdbName=2006_register+Federal+Register%2C+Volume+71+%282006%29&WAISdbName=2005_register+Federal+Register%2C+Volume+70+%282005%29&WAISdbName=2004_register+Federal+Register%2C+Volume+69+%282004%29&WAISdbName=2003_register+Federal+Register%2C+Volume+68+%282003%29&WAISdbName=2002_register+Federal+Register%2C+Volume+67+%282002%29&WAISdbName=2001_register+Federal+Register%2C+Volume+66+%282001%29&WAISdbName=2000_register+Federal+Register%2C+Volume+65+%282000%29&WAISdbName=1999_register+Federal+Register%2C+Volume+64+%281999%29&WAISdbName=1998_register+Federal+Register%2C+Volume+63+%281998%29&WAISdbName=1997_register+Federal+Register%2C+Volume+62+%281997%29&WAISdbName=1996_register+Federal+Register%2C+Volume+61+%281996%29&WAISdbName=1995_register+Federal+Register%2C+Volume+60+%281995%29&date1=&date2=&op=%3D&date3=&WAISqueryRule=%28%24WAISqueryString%29+AND+%28section%3D%24sect+OR+section%3D%24sect1+OR+section%3D%24sect2+OR+section%3D%24sect3+OR+section%3D%24sect4+OR+section%3D%24sect5+OR+section%3D%24sect6+OR+section%3D%24sect7%29+AND+%28%28date+%3E%3D+%24date1+AND+date+%3C%3D+%24date2%29+OR+%28date+%24op+%24date3%29%29&WAIStemplate=multidb_results.html&WrapperTemplate=fr_wrapper.html&WAISqueryString=Osama+Bin+Ladin&Submit.=Submit&WAISmaxHits=50");
      System.out.println("===========RECEIVING DATA==========");
      while ((tempData = readData.readLine()) != null) {
         System.out.println(tempData);
      }
      System.out.println("===========DONE WITH DATA==========");
      sock.close();
   }


Thank you Nate and bleb for your amazing help so far  Smiley

w00t, i'm winning
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 748
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2010-04-19 20:43:36 »

1  
2  
3  
4  
...
      sendData.writeBytes("Content-Length: 1523\r\n");
      sendData.writeBytes("POST /cgi-bin/multidb.cgi HTTP/1.0\r\n");
      sendData.writeBytes("A LOT OF DATA");


You are making a 2nd request there. It must be a blank line.

1  
2  
3  
4  
...
      sendData.writeBytes("Content-Length: 1523\r\n");
      sendData.writeBytes("\r\n");
      sendData.writeBytes("A LOT OF DATA");

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!