Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   POST to secure server (http://www.velocityreviews.com/forums/t959087-post-to-secure-server.html)

RVic 03-26-2013 11:46 AM

POST to secure server
 
I have an application that must run in windows which must post to a servletrunning on a secure server. I can WinSCP into that server, and I can use PuTTY to ssh into that server. But I am quite clueless how I can post to a servlet there? Is there something I can pass in posting, or something I do with my URLConnection or URL variable to allow me access as I get with WinSCP or PuTTY? Thank you.

URL postURL = new URL(servletURL);
HttpURLConnection conn = (HttpURLConnection) postURL.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.connect();
StringBuilder sb = new StringBuilder();
sb.append(vtkey+"="+ paramval);
out.write(sb.toString());
out.flush();

markspace 03-26-2013 04:51 PM

Re: POST to secure server
 
On 3/26/2013 4:46 AM, RVic wrote:
> I have an application that must run in windows which must post to a
> servlet running on a secure server. I can WinSCP into that server,
> and I can use PuTTY to ssh into that server. But I am quite clueless
> how I can post to a servlet there?

....
> out.write(sb.toString());
> out.flush();


out.close();

Generally speaking, you must close the TCP connection or the server
won't "recognize" that you are done sending data.

I seem to recall that Java's built-in HttpConnection is kind of
primitive and doesn't really implement everything you'd expect for a
real HTTP connection. In particular, I think you have to handle cookies
yourself, which might be causing the server code to break if it doesn't
see the cookies it's expecting.

I think the Apache Commons library has an HTTP class that handles things
more automagically. Try their HttpClient if you can't figure out what
the built-in version needs:

<http://hc.apache.org/>


Roedy Green 03-27-2013 02:00 AM

Re: POST to secure server
 
On Tue, 26 Mar 2013 04:46:07 -0700 (PDT), RVic <rvince99@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>I have an application that must run in windows which must post to a servlet=
> running on a secure server. I can WinSCP into that server, and I can use P=
>uTTY to ssh into that server. But I am quite clueless how I can post to a s=
>ervlet there? Is there something I can pass in posting, or something I do w=
>ith my URLConnection or URL variable to allow me access as I get with WinSC=
>P or PuTTY? Thank you.


for sample code see http://mindprod.com/jgloss/http.html
--
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes
that went into it. Something was more important than sensory pleasure --
nobody would invent a chair or dish that smelled bad or that made horrible
noises -- why were motors invented noisy? How could they possibly be
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner.
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural

RVic 03-27-2013 12:34 PM

Re: POST to secure server
 
Thanks for all your responses. I'm finding out from the client that they have no exposed URLs that my servlet call can come in on, and they need to somehow provide that on their end.

OTOH, because I need to get this project done ad get it done beyond the crawl of academic beureaucracy, couldn't I simply read and write to a socket (in lie of a servlet), forget about the post, just wrap and unwrap the parameters I would otherwise post, that I could access somehow with ssh through java?

Thanks in advance.

RVic 03-27-2013 01:24 PM

Re: POST to secure server
 
If I ssh in.....trying to think this through, how might I replicate a post to a servlet? Not sure on this last step, wouldn't I need a socket listenerrunning on the target where I otherwise would have had a servlet running? Thanks for your thoughts and ideas here.

Silvio 03-27-2013 01:37 PM

Re: POST to secure server
 
On 03/26/2013 12:46 PM, RVic wrote:
> I have an application that must run in windows which must post to a servlet running on a secure server. I can WinSCP into that server, and I can use PuTTY to ssh into that server. But I am quite clueless how I can post to a servlet there? Is there something I can pass in posting, or something I do with my URLConnection or URL variable to allow me access as I get with WinSCP or PuTTY? Thank you.
>
> URL postURL = new URL(servletURL);
> HttpURLConnection conn = (HttpURLConnection) postURL.openConnection();
> conn.setRequestMethod("POST");
> conn.setDoOutput(true);
> conn.setUseCaches(false);
> conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
> conn.connect();
> StringBuilder sb = new StringBuilder();
> sb.append(vtkey+"="+ paramval);
> out.write(sb.toString());
> out.flush();
>


There are two options: you either post via HTTP or HTTPS directly to the
servlet (possibly passing through reverse proxies or NAT at the server
side) or you need to create an SSH tunnel first and use the tunnel to
reach the servlet.

The former will only work if the servlet is accessible from the outer
world. The latter will require you to create the tunnel from the client
side using the SSH connection for transport. Putty can create the tunnel
for you but this requires setting up the tunnel manually. If you want to
create the tunnel from inside the application you would need an SSH
library. JSCH would be a good candidate since it is open source and it
supports tunnelling. Having set up the tunnel you can simply post to
localhost.

Silvio


RVic 03-27-2013 01:45 PM

Re: POST to secure server
 
Thank you. I was thinking I would have to create a socket listener on the server side once I had an ssh connection set up with Jsch. I am looking through the Jsch examples (I am enclosing one in particular caled ViaHTTP) but I cannot, for the life of me, see how I would then perform my Post within that framework? Can you please explain further? Thank you!


String proxy_host;
int proxy_port;

try{
JSch jsch=new JSch();

String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host=JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name")+
"@localhost");
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);

Session session=jsch.getSession(user, host, 22);

String proxy=JOptionPane.showInputDialog("Enter proxy server",
"hostname:port");
proxy_host=proxy.substring(0, proxy.indexOf(':'));
proxy_port=Integer.parseInt(proxy.substring(proxy. indexOf(':')+1));

session.setProxy(new ProxyHTTP(proxy_host, proxy_port));

// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);

session.connect();

Channel channel=session.openChannel("shell");

channel.setInputStream(System.in);
channel.setOutputStream(System.out);

channel.connect();
}
catch(Exception e){
System.out.println(e);
}

Silvio 03-27-2013 02:08 PM

Re: POST to secure server
 
On 03/27/2013 02:45 PM, RVic wrote:
> Thank you. I was thinking I would have to create a socket listener on the server side once I had an ssh connection set up with Jsch. I am looking through the Jsch examples (I am enclosing one in particular caled ViaHTTP) but I cannot, for the life of me, see how I would then perform my Post within that framework? Can you please explain further? Thank you!
>
>
> String proxy_host;
> int proxy_port;
>
> try{
> JSch jsch=new JSch();
>
> String host=null;
> if(arg.length>0){
> host=arg[0];
> }
> else{
> host=JOptionPane.showInputDialog("Enter username@hostname",
> System.getProperty("user.name")+
> "@localhost");
> }
> String user=host.substring(0, host.indexOf('@'));
> host=host.substring(host.indexOf('@')+1);
>
> Session session=jsch.getSession(user, host, 22);
>
> String proxy=JOptionPane.showInputDialog("Enter proxy server",
> "hostname:port");
> proxy_host=proxy.substring(0, proxy.indexOf(':'));
> proxy_port=Integer.parseInt(proxy.substring(proxy. indexOf(':')+1));
>
> session.setProxy(new ProxyHTTP(proxy_host, proxy_port));
>
> // username and password will be given via UserInfo interface.
> UserInfo ui=new MyUserInfo();
> session.setUserInfo(ui);
>
> session.connect();
>
> Channel channel=session.openChannel("shell");
>
> channel.setInputStream(System.in);
> channel.setOutputStream(System.out);
>
> channel.connect();
> }
> catch(Exception e){
> System.out.println(e);
> }
>



I am guessing you meant this as a reply to my post...

If you setup a tunnel using JSCH (which requires adding an addition
connection property string to the connection that specifies the server
destination host:port and the local port on the client side then any
communication going through localhost:port on the client will be
tunnelled via SSH and reach the destination host:port on the server side.
So assuming that there is an existing servlet on the server side that is
listening for HTTP requests on port 80 on the same box you make the SSH
connection to you server host:port would be localhost:80 and you could
use any desired port on the client side, lets say 8080. After that you
can use new URL("http://localhost:8080/path/to/the/servlet/") and do a
normal post from the client. The tunnel will automagically make your
request reach the servlet.

Despite what others have said the standard HTTP client in Java works
fine and will probably give you what you need. If the servlet might barf
on the URL because of the localhost:8080 you could even use any
imaginable URL like new URL("http://www.whatever.com/path/to/servlet/")
and use the proxy option of URL#openConnection to get traffic through
localhost:8080.

Good luck,

Silvio


RVic 03-27-2013 03:39 PM

Re: POST to secure server
 
Silvio, Thank you -- but I am a little confused by what you say. Right now, using JSCH I can connect with:

JSch jsch = new JSch();
Session session = jsch.getSession("me"/*user*/, "moonprod"/*serverURL*/);
session.setPort(22);
session.setPassword(password);
session.connect();

And the servlet I must reach is on Tomcat on the server on port 28080 which would be hit as http://moonprod:28080/servlets/myservlet
What are you saying, below, that I should change? Thank you!



<<If you setup a tunnel using JSCH (which requires adding an addition
connection property string to the connection that specifies the server
destination host:port and the local port on the client side then any
communication going through localhost:port on the client will be
tunnelled via SSH and reach the destination host:port on the server side.
So assuming that there is an existing servlet on the server side that is
listening for HTTP requests on port 80 on the same box you make the SSH
connection to you server host:port would be localhost:80 and you could
use any desired port on the client side, lets say 8080. After that you
can use new URL("http://localhost:8080/path/to/the/servlet/") and do a
normal post from the client. The tunnel will automagically make your
request reach the servlet. >>

RVic 03-27-2013 08:26 PM

Re: POST to secure server
 
lipsa, Yes I do not know what silvio means -- not even sure why I would need to port forward it? Thank you.


All times are GMT. The time now is 05:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.