How to send XML-RPC request with HTTPS? - java

I am new to XML-RPC and may be my question is silly but I can't find any information to help me for that...
So here it is : I am using this java code to send a XML file through a XML-RPC request using HTTP.
public static void sendXML(String file, String host, String port, String url) throws IOException{
Socket socket = new Socket(host, Integer.parseInt(port));
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
StringBuffer header = new StringBuffer();
header.append("POST "+url+" HTTP/1.0\n");
header.append("Content-Type: text/xml\n");
header.append("Content-Length: "+(new File(file).length()+2)+"\n");
header.append("\n");
byte[] buffer = header.toString().getBytes("UTF-8");
out.write(buffer);
InputStream src = new FileInputStream(file);
buffer = new byte[1024];
int b = 0;
while((b = src.read(buffer)) >= 0){
out.write(buffer, 0, b);
}
buffer = "\n\n".getBytes("UTF-8");
out.write(buffer);
out.close();
src.close();
in.close();
out.flush();
socket.close();
}
In this code, the XML file is already created, containing the method called and all the parameters.
This solution works fine but I am asking to make it compatible for a HTTPS protocol.
Do I need to only change the line
header.append("POST "+url+" HTTP/1.0\n");
in
header.append("POST "+url+" HTTPS/1.0\n");
?
Or should I use the Apache library https://ws.apache.org/xmlrpc/client.html ?
Or may be is there any simpler solution in java language ?
Thank you all for your help

Related

How to save a file sent from a http response without including the header

I am trying to write a program in java which is able to download a file from a URL. I want to do this without using an URLConnection, instead i am just using TCP sockets. I have succeeded in sending the GET request and picking up the server's response, but i can't seem to get my head around saving the file from the response without the http-header(just the file).
import java.net.*;
import java.io.*;
public class DownloadClient {
public static void main(String[] args) {
try {
if (args.length != 3) {
System.out.println(
"Use: java DownloadClient <host> <port> <filename/path>"
);
} else {
// Sorting out arguments from the args array
String host;
int port;
String filename;
if (args[0].charAt(args[0].length()-1) == '/') {
host = args[0].substring(0,args[0].length()-1);
} else {
host = args[0];
}
port = Integer.parseInt(args[1]);
if (args[2].charAt(0) == '/') {
filename = args[2];
} else {
filename = "/"+args[2];
}
Socket con = new Socket(args[0], Integer.parseInt(args[1]));
// GET request
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(con.getOutputStream(), "UTF8")
);
out.write("GET "+filename+" HTTP/1.1\r\n");
out.write("Host: "+host+"\r\n");
out.write("User-Agent: Java DownloadClient\r\n\r\n");
out.flush();
InputStream in = con.getInputStream();
BufferedReader =
OutputStream outputFile = new FileOutputStream(
filename.substring(filename.lastIndexOf('/')+1)
);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = in.read(buffer)) > 0) {
outputFile.write(buffer, 0, bytesRead);
buffer = new byte[1024];
}
outputFile.close();
in.close();
con.close();
}
} catch (IOException e) {
System.err.println(e);
}
}
}
I guess that i should somehow look for \r\n\r\n as it indicates the empty line just before the content begins. So far this program creates a file which contains all of the http-response.
The recommended way to do this is to NOT try to talk to a web server using a plain Socket. Use one of the existing client-side HTTP stack; e.g. the standard HttpUrlConnection stack or the Apache HttpClient stack.
If you insist on talking using a plain socket, then it is up to you to process / deal with the "Header" lines in any response ... and everything else ... in accordance with the HTTP specification.
I guess that I should somehow look for \r\n\r\n as it indicates the empty line just before the content begins.
Yup ...
And you also potentially need to deal with the server sending a compressed response, an response using an unexpected character set, a 3xx redirect, and so on.

Java request file, send file (Client-server)

I'm making a Client-Server. I've gotten as far as that the server can send a hardcoded file, but not a client specified. I will have to send only text files. As far as I have understood: the clients firstly sends the file name and then, the server sends it, nothing complicated, but I'm getting all kinds of errors, this code is getting a connection reset/socket closed error. The main problem is, that hadn't got much time to research networking.
Ill appreciate any help I can get.
EDIT.
I found a work around, closing a stream causes the socket to close, why is that? It shouldn't happen, should it?
Server Side:
InputStream sin=newCon.getInputStream();
DataInputStream sdata=new DataInputStream(sin);
location=sdata.readUTF();
//sdata.close();
//sin.close();
File toSend=new File(location);
byte[] array=new byte[(int)toSend.length()];
FileInputStream fromFile=new FileInputStream(toSend);
BufferedInputStream toBuffer=new BufferedInputStream(fromFile);
toBuffer.read(array,0,array.length);
OutputStream out=newCon.getOutputStream(); //Socket-closed...
out.write(array,0,array.length);
out.flush();
toBuffer.close();
newCon.close();
ClientSide:
int bytesRead;
server=new Socket(host,port);
OutputStream sout=server.getOutputStream();
DataOutputStream sdata=new DataOutputStream(sout);
sdata.writeUTF(interestFile);
//sdata.close();
//sout.close();
InputStream in=server.getInputStream(); //socket closed..
OutputStream out=new FileOutputStream("data.txt");
byte[] buffer=new byte[1024];
while((bytesRead=in.read(buffer))!=-1)
{
out.write(buffer,0,bytesRead);
}
out.close();
server.close();
Try reading the file in chunks from Server while writing to client output stream rather than creating a temp byte array and reading entire file into memory. What if requested file is large? Also close the new Socket on server-side in a finally block so socket is closed even if an exception is thrown.
Server Side:
Socket newCon = ss.accept();
FileInputStream is = null;
OutputStream out = null;
try {
InputStream sin = newCon.getInputStream();
DataInputStream sdata = new DataInputStream(sin);
String location = sdata.readUTF();
System.out.println("location=" + location);
File toSend = new File(location);
// TODO: validate file is safe to access here
if (!toSend.exists()) {
System.out.println("File does not exist");
return;
}
is = new FileInputStream(toSend);
out = newCon.getOutputStream();
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = is.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
} finally {
if (out != null)
try {
out.close();
} catch(IOException e) {
}
if (is != null)
try {
is.close();
} catch(IOException e) {
}
newCon.close();
}
If you use Apache Common IOUtils library then you can reduce much of the code to read/write files to streams. Here 5-lines down to one line.
org.apache.commons.io.IOUtils.copy(is, out);
Note that having a server that serves files by absolute path to remote clients is potentially dangerous and the target file should be restricted to a given directory and/or set of file types. Don't want to serve out system-level files to unauthenticated clients.

Concurrency Issue Using Multithreading in Java

I have written a code for Server and Client in Java. Clients are able to download the files from the server and the server should also be able to provide files to clients Concurrently.
For this purpose I have used multithreading in Server.
It is working perfectly fine for one client but while using threads for every client it seems to be not working properly in my Code.
As the files are not getting downloaded correctly, are corrupt and are of varying sizes for different clients.
After accepting from a client, I am creating a new Thread for serving it the file
Code for server -
public static void send(String pathname) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException
{
try
{
System.out.println("....");
byte[] buf = new byte[1024];
OutputStream os = sock.getOutputStream();
//PrintWriter writer = new PrintWriter(os);
System.out.println("...11.");
BufferedOutputStream out = new BufferedOutputStream(os, 1024);
int i=0;
System.out.println("hi1");
File fp = new File(pathname);
System.out.println("hi2");
RandomAccessFile ra = new RandomAccessFile(fp,"r");
System.out.println("hi3");
long bytecount=1024;
////////////////
while((i=ra.read(buf, 0, 1024)) != -1)
{
System.out.println("hi");
bytecount += 1024;
System.out.println("hi6");
out.write(buf, 0, i);
System.out.println("hi7");
out.flush();
}
System.out.println("bye");
//os.flush();
//out.close();
ra.close();
//sock.close();
}
catch(IOException ex)
{
}
}
And code for client for file receiving is
public void run() {
try{
byte[] b = new byte[1024];
int len = 0;
long bytcount = 1024;
File fp = new File(path);
RandomAccessFile ra=new RandomAccessFile(fp,"rw");
ra.seek(0);
InputStream is = sock.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
while ((len = is.read(b, 0, 1024)) != -1) {
bytcount = bytcount + 1024;
//decrypt
ra.write(b, 0, len);
}
//is.close();
//ra.close();
//sock.close();
}
catch(IOException ex){
ex.printStackTrace();
}
// throw new UnsupportedOperationException("Not supported yet.");
}
}
I am not getting out what's wrong here. Please help
Many many thanx in advance
send is static, which suggests to me that you have only one socket, named sock, or the following would not compile.
public static void send(String pathname) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException
{
try
{
System.out.println("....");
byte[] buf = new byte[1024];
OutputStream os = sock.getOutputStream();
//PrintWriter writer = new PrintWriter(os);
System.out.println("...11.");
If that is the case, it's hard to see how delivery to >1 client will be reliable. You will surely need one socket per client to perform concurrent deliveries? More code is needed to be sure of this, but it looks like the design of socket handling may need to be revised.
Your first problem is that you are using an OutputStream on the server and a Reader on the client. the client should be using InputStreams, not Readers.

java socket download image

I have done some research, also checked answer on stackoverflow. However i just cant get my code right, please help. the code can run, but i cant get the image, it shows 0kb.
Socket socket = new Socket(addr, port);
byte [] buffer = new byte[1024];
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("GET " + url.getFile() + " HTTP/1.0\r\n");
writer.println("HOST:" + url.getHost() + "\r\n");
writer.println("\r\n");
DataInputStream in = new DataInputStream(socket.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
int n = 0;
while (-1!=(n=in.read(buffer)))
{
out.write(buffer, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream("0.jpeg");
fos.write(response);
fos.close();
}catch (Exception e){
System.out.println(e.toString());
}
Using raw sockets to perform an HTTP GET is much more complicated than necessary.
I recommend using an HTTP client like the one from Apache or you can use java.net.URLConnection. See How do I do a HTTP GET in Java? or Using java.net.URLConnection to fire and handle HTTP requests
Your code doesn't have any obvious flaws. If you're getting a zero length file, it's because you aren't sending anything.
BTW you don't need the ByteArrayOutputStream. You can write everything you read directly to the FileOutputStream. Saves both time and space.

Socket-transferred file: contents are empty

I am working on transferring a file between two computers over a socket. Everything seems to work, but when I look at the contents of the retrieved file, it is empty. What am I doing wrong?
Here is my server-side code. The file foobar.txt exists, and its contents are "hello world!".
try{
ServerSocket ssock = new ServerSocket(12345);
Socket sock = ssock.accept();
//here I get the filename from the client, but that works fine.
File myFile = new File("foobar.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
} catch (Exception e){
e.printStackTrace();
}
And here is my client code:
try {
Socket socket = new Socket(host, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.print("get foobar.txt\r\n");
out.flush();
byte[] streamIn = new byte[1024];
InputStream in = socket.getInputStream();
FileOutputStream file_src = new FileOutputStream("foobar.txt");
BufferedOutputStream file_writer = new BufferedOutputStream(file_src);
int i;
while ((i = in.read()) != -1) {
file_writer.write(i);
}
file_writer.flush();
file_writer.close();
file_src.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
Solved
Since I am using multiple threads and multiple sockets and testing all connections on one machine, I was simply running into a problem where the client (which has both the client and server code in it) would connect with itself instead of the other client. Changing the file transfer port for the different running clients got this all to work. Thanks for everyone who had a look at this and gave me some suggestions.
Maybe you're closing the wrong socket on the client. When you close the socket, you're closing the class field this.socket instead of the local variable socket.
Also, when you close the output stream to the file, you don't have to close both the BufferedOutputStream and the FileOutputStream. The FileOutputStream is automatically closed when the BufferedOutputStream is closed.
One more thing---you don't have to flush an output stream before closing it. When you call close() the stream is automatically flushed.
In addition to what everyone else has said, you are ignoring the result of bis.read(). It isn't guaranteed to fill the buffer. See the Javadoc.
The correct way to copy streams in Java, which you should use at both ends, is this:
byte[] buffer = new byte[8192]; // or whatever
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
The only thing I think of that is that you actually never start receiving the file because the server-side doesn't read the command ("get foobar.txt"), so the client-side freezes on sending the command.
The existence of the file at the client-side might be from previous tests.
But, I'm not sure this is the problem. It's just a try to help.

Categories

Resources