473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Anyone help with emulating Java URLConnection?

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/
Jul 17 '05 #1
4 3917
On Fri, 30 Apr 2004 15:04:10 +1200
Jochen Daum <jo*********@cabletalk.co.nz> wrote:
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 fsockopen() to be able to write to your $handle.
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.


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.

Jul 17 '05 #2
Hi Kelly,

On Thu, 29 Apr 2004 23:59:53 -0400, Kelly Thompson
<kt*************@hotmail.com> wrote:
On Fri, 30 Apr 2004 15:04:10 +1200
Jochen Daum <jo*********@cabletalk.co.nz> wrote:
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 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.
Why not?


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.

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: (...)

I think I'm missing that, but thats a different problem.
$host = 'example.com';
$path = '/path/to/script';
$port = 443 /* ssl */

(..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/
Jul 17 '05 #3
On Fri, 30 Apr 2004 16:20:31 +1200
Jochen Daum <jo*********@cabletalk.co.nz> wrote:
Hi Kelly,
Hi Jochen.
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:

(...)

I think I'm missing that, but thats a different problem.


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

[snip java source]
I'm unsure, if this connection contains HTTP headers and if and what
request method is used.
I'm unsure either.
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);
Aren't you missing a curl_exec($ch)?

but I get an 503 as an answer.


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.

Jul 17 '05 #4
Hi,

On Fri, 30 Apr 2004 01:35:31 -0400, Kelly Thompson
<kt*************@hotmail.com> wrote:
On Fri, 30 Apr 2004 16:20:31 +1200
Jochen Daum <jo*********@cabletalk.co.nz> wrote:
Hi Kelly,
Hi Jochen.
>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:

(...)

I think I'm missing that, but thats a different problem.


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


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


....someone in the java group said, it uses a POST request. Just need
to figure out how it is filled.
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);


Aren't you missing a curl_exec($ch)?


Left it out, so you don't have to read.

but I get an 503 as an answer.


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


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.

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.


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/
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
URL: http://valsignalandet.com/cookiegrab.php?name=valIdentifier This page produces a cookie value and sends it back as HTML using PHP and Javascript to obtain it. This URL will be used as the...
2
by: Jan Meyer | last post by:
When I run the code below as an applet I get this message: Hello java there was a problem access denied (java.net.SocketPermission yahoo.com resolve) But when I take the same code for...
0
by: ewright66 | last post by:
I'm trying to access CiscoWorks 2000 from a home PC via VPN on a Win XP Home Edition computer. CiscoWorks requires Java JRE 1.3.1 to apparently open applets. When I try to bring up a Topology Map...
1
by: Jason Chu | last post by:
I've written a java applet to write multipart form to post to asp.net page. It works, asp.net can read all the form values and things i wanted from the socket posting. Now I want to get some sort...
24
by: crazystone82 | last post by:
Hi all, please send me a source code to transfer a file to a server running on another pc in the LAN...using JAVA .By getting the source path and destination path through textfield implemented...
1
by: Meiaad | last post by:
I have a java project. this project is stock market monitoring system. our jop is to download the source of the html page using eclips. and i success to do that but the problem is how can i search...
7
madhoriya22
by: madhoriya22 | last post by:
Hi, I am trying to read a file sent from client to server. Here is how I am trying it .... String filePath = request.getParameter("SelectCSVFile"); System.out.println("path:::---> "+filePath);...
5
Atli
by: Atli | last post by:
Hi everybody. After years of C# and PHP, I'm finally returning to Java. My goal is to create a Java program capable of sending images to a PHP Photo Album on my web server. Right now, however,...
2
hsn
by: hsn | last post by:
hello everyone i have posted a question before and i was told to read about scriptengine in java after reading about it and studying it i still was not able to solve the issue which i am facing. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.