Connecting Tech Pros Worldwide Help | Site Map

Anyone help with emulating Java URLConnection?

Jochen Daum
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi,

I have to emulate a "file upload" to a Java Servlet which is done with
the class URLConnection.

The java source basically does

URLConnection conn = theurl.openConnection("https://somesite");
w = conn.getOutputStream();
w.write(...)


A thing I'm unclear of is if this connection is actually encrypted
over SSL. The other thing is if this just sends the bytes or also any
HTTP headers

I can think of three possible solutions:

Use fopen:

- I would have to recompile PHP for that and also it says:

"failed to open stream: HTTP wrapper does not support writeable
connections"

Use sockets:

- I have bad experiences with it being unreliable, if you read from
the connection and there are no bytes send. The timeout doesn't seem
to work. Also, I wouldn't know how the SSL would work with that.

Use curl:

- I would expect curl to always send a HTTP header, so I don't know if
its compatible.


Anyone here with Java experience that could help me?

Jochen

--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Kelly Thompson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Anyone help with emulating Java URLConnection?


On Fri, 30 Apr 2004 15:04:10 +1200
Jochen Daum <jochen.daum@cabletalk.co.nz> wrote:
[color=blue]
> A thing I'm unclear of is if this connection is actually encrypted
> over SSL. The other thing is if this just sends the bytes or also
> any HTTP headers
>
> I can think of three possible solutions:
>
> Use fopen:
>
> - I would have to recompile PHP for that and also it says:
>
> "failed to open stream: HTTP wrapper does not support writeable
> connections"[/color]

Use fsockopen() to be able to write to your $handle.
[color=blue]
> Use sockets:
>
> - I have bad experiences with it being unreliable, if you read from
> the connection and there are no bytes send. The timeout doesn't seem
> to work. Also, I wouldn't know how the SSL would work with that.[/color]

Why not?

I think that in a few words you want a function do_post(). If PHP
was compiled against SSL libraries, then fsockopen() will support
``ssl://''. A rough implementation:


$host = 'example.com';
$path = '/path/to/script';
$port = 443 /* ssl */

/* $data=array() is your data source */

function do_post($host, $path, $port, $data) {

foreach($data AS $key => $val) {
$post.= $key . "=" . urlencode($val) . "&";
}

if(! $handle=fsockopen("ssl://" . $host,
$port, $errno,
$errstr, $timeout = 30) )
return -1; /* connection failed */

$req ="POST {$path} HTTP/1.1\r\n";
$req.="Host: {$host}\r\n";
$req.="Content-type: application/x-www-url-encoded\r\n";
$req.="Content-length: ".strlen($post)."\r\n";
$req.="Connection: close\r\n\r\n";
$req.="{$post}\r\n\r\n";

if ( fwrite($handle,$req,strlen($req)) ) {
while( !feof($handle) )
$buf.=fgetc($handle);
} else return -2; /* error reading from socket */

fclose($handle);
return $buf;
}

You have to arrange to add your file and set ``content-type''
appropriately. Look up (HTTP RFC, if you will) for details on how to
do that.

Jochen Daum
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Anyone help with emulating Java URLConnection?


Hi Kelly,

On Thu, 29 Apr 2004 23:59:53 -0400, Kelly Thompson
<kthompson_11_11@hotmail.com> wrote:
[color=blue]
>On Fri, 30 Apr 2004 15:04:10 +1200
>Jochen Daum <jochen.daum@cabletalk.co.nz> wrote:
>[color=green]
>> A thing I'm unclear of is if this connection is actually encrypted
>> over SSL. The other thing is if this just sends the bytes or also
>> any HTTP headers
>>
>> I can think of three possible solutions:
>>[/color][/color]
(...)
[color=blue][color=green]
>> Use sockets:
>>
>> - I have bad experiences with it being unreliable, if you read from
>> the connection and there are no bytes send. The timeout doesn't seem
>> to work. Also, I wouldn't know how the SSL would work with that.[/color]
>
>Why not?[/color]

Well it didn't when I used sockets to access a serial modem. If the
modem didn't send a byte, it would just never time out.
[color=blue]
>
>I think that in a few words you want a function do_post(). If PHP
>was compiled against SSL libraries, then fsockopen() will support
>``ssl://''. A rough implementation:[/color]
(...)

I think I'm missing that, but thats a different problem.[color=blue]
>
>$host = 'example.com';
>$path = '/path/to/script';
>$port = 443 /* ssl */
>[/color]
(..doing a HTTP POST...)


because of the Java source I have:

public void putFile(String thedir, String thefile) throws Exception {
System.out.println(getStorePath()+thefile);
URL theurl = new URL(getStorePath()+thefile.replace(' ','_'));
System.out.println(theurl.toString());
URLConnection conn = theurl.openConnection();
// setting the authorization header doesn't seem to work - it
remains as null
// and then causes errors
// conn.setRequestProperty("Authorization","Basic "+auth);
conn.setDoOutput(true);
URL newurl = conn.getURL();
System.out.println(newurl.toString());
// conn.connect();
connect(new FileInputStream(new File(thedir,
thefile)),conn.getOutputStream());
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String response = in.readLine();
System.out.println(response);

I'm unsure, if this connection contains HTTP headers and if and what
request method is used.

I also can't quite figure out what the fieldname might be. There is
nothing in connect(in,out) that does anything else but copying. I have
tried to just post 'thefile' with curl:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
//curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $thefile);
curl_setopt($ch, CURLOPT_USERPWD, "xxx:xxx");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);


but I get an 503 as an answer.

Thanks for your help so far.

Jochen








--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Kelly Thompson
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Anyone help with emulating Java URLConnection?


On Fri, 30 Apr 2004 16:20:31 +1200
Jochen Daum <jochen.daum@cabletalk.co.nz> wrote:
[color=blue]
> Hi Kelly,[/color]

Hi Jochen.
[color=blue][color=green]
> >I think that in a few words you want a function do_post(). If PHP
> >was compiled against SSL libraries, then fsockopen() will support
> >``ssl://''. A rough implementation:[/color]
> (...)
>
> I think I'm missing that, but thats a different problem.[/color]

If you're missing, then yes, you need to recompile.

[snip java source]
[color=blue]
> I'm unsure, if this connection contains HTTP headers and if and what
> request method is used.[/color]

I'm unsure either.
[color=blue]
> I also can't quite figure out what the fieldname might be. There is
> nothing in connect(in,out) that does anything else but copying. I
> have tried to just post 'thefile' with curl:
>
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_URL,$url);
> //curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
> //curl_setopt($ch, CURLOPT_POST, 1);
> curl_setopt($ch, CURLOPT_POSTFIELDS, $thefile);
> curl_setopt($ch, CURLOPT_USERPWD, "xxx:xxx");
> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
> curl_setopt($ch, CURLOPT_TIMEOUT, 100);[/color]

Aren't you missing a curl_exec($ch)?
[color=blue]
>
> but I get an 503 as an answer.[/color]

503 is Service Unavailable: ``The server cannot respond due to
temporary overloading or maintenance.'' Is your server too busy?

You can also install curl and use system() to perform the task, like
you do using the console. http://curl.haxx.se/docs/httpscripting.html

Maybe this URL can help you somewhat. Also, if you unsure of any HTTP
request, try doing GET first since GET is simpler, once you get some
good results, advance to try a POST without attaching a file, then try
attaching a FILE. I mean, make sure you can talk to the server, before
you try any conversation.

Jochen Daum
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Anyone help with emulating Java URLConnection?


Hi,

On Fri, 30 Apr 2004 01:35:31 -0400, Kelly Thompson
<kthompson_11_11@hotmail.com> wrote:
[color=blue]
>On Fri, 30 Apr 2004 16:20:31 +1200
>Jochen Daum <jochen.daum@cabletalk.co.nz> wrote:
>[color=green]
>> Hi Kelly,[/color]
>
>Hi Jochen.
>[color=green][color=darkred]
>> >I think that in a few words you want a function do_post(). If PHP
>> >was compiled against SSL libraries, then fsockopen() will support
>> >``ssl://''. A rough implementation:[/color]
>> (...)
>>
>> I think I'm missing that, but thats a different problem.[/color]
>
>If you're missing, then yes, you need to recompile.
>[/color]

But I have curl compiled, so tahts sorted, because...
[color=blue]
>[snip java source]
>[color=green]
>> I'm unsure, if this connection contains HTTP headers and if and what
>> request method is used.[/color]
>
>I'm unsure either.
>[/color]

....someone in the java group said, it uses a POST request. Just need
to figure out how it is filled.
[color=blue][color=green]
>> I also can't quite figure out what the fieldname might be. There is
>> nothing in connect(in,out) that does anything else but copying. I
>> have tried to just post 'thefile' with curl:
>>
>> $ch = curl_init();
>> curl_setopt($ch, CURLOPT_URL,$url);
>> //curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
>> curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
>> //curl_setopt($ch, CURLOPT_POST, 1);
>> curl_setopt($ch, CURLOPT_POSTFIELDS, $thefile);
>> curl_setopt($ch, CURLOPT_USERPWD, "xxx:xxx");
>> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
>> curl_setopt($ch, CURLOPT_TIMEOUT, 100);[/color]
>
>Aren't you missing a curl_exec($ch)?
>[/color]

Left it out, so you don't have to read.
[color=blue][color=green]
>>
>> but I get an 503 as an answer.[/color]
>
>503 is Service Unavailable: ``The server cannot respond due to
>temporary overloading or maintenance.'' Is your server too busy?[/color]

I think, I rather haven't figured out the URL. I'm replacing a manual
form (with the java applet built-in and the form always works.
[color=blue]
>
>You can also install curl and use system() to perform the task, like
>you do using the console. http://curl.haxx.se/docs/httpscripting.html
>
>Maybe this URL can help you somewhat. Also, if you unsure of any HTTP
>request, try doing GET first since GET is simpler, once you get some
>good results, advance to try a POST without attaching a file, then try
>attaching a FILE. I mean, make sure you can talk to the server, before
>you try any conversation.[/color]

Thanks for your help,

Jochen

--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Closed Thread