I've got a legacy application that writes to an OutputStream, and I'd like to have the contents of this stream uploaded as a file to a Servlet. I've tested the Servlet, which uses commons-fileupload, using JMeter and it works just fine.
I would use Apache HttpClient, but it requires a File rather than just an output stream. I can't write a file locally; if there was some in-memory implementation of File perhaps that might work?
I've tried using HttpURLConnection (below) but the server responds with "MalformedStreamException: Stream ended unexpectedly".
URL url = new URL("http", "localhost", 8080, "/upload");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
String boundary = "---------------------------7d226f700d0";
connection.setRequestProperty("Content-Disposition", "form-data; name=\"file\"");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.setChunkedStreamingMode(0);
connection.connect();
OutputStream out = connection.getOutputStream();
byte[] boundaryBytes =("--" + boundary + "\r\n").getBytes();
out.write(boundaryBytes);
//App writes to outputstream here
out.write("\r\n".getBytes());
out.write(("--"+boundary+"--").getBytes());
out.write("\r\n".getBytes());
out.flush();
out.close();
connection.disconnect();
The PostMethod allows you to set a RequestEntity, which is an interface which you can implement. you just need to implement the RequestEntity.writeRequest method appropriately.
Or, if you want HttpClient to handle the multi-part stuff for you, you could use MultipartRequestEntity with a custom Part.
Related
I'm having problems doing a Postman REST call copying a REST call in Java.
I tried to set request properties on Postman the same way they're set in Java, but it's not working.
I have to send a base64 string with this call (i put in italic the code line where this is done in Java code)
URLConnection connection = new URL(url + content).openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(timeout);
((HttpURLConnection) connection).setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
OutputStream output = connection.getOutputStream();
JSONObject conf = new JSONObject();
conf.put("signedEvidence", String.format("%s", baos));
*output.write(conf.toString().getBytes());*
output.flush();
checkHttpStatus(connection);
I configured Postman like this:
And i receive this answer:
EDIT - In few words: the REST call works fine in Java, but i need to do some of these calls in Postman with my own variable (the service i'm calling do some works with base64 string i pass him).
EDIT2 - Main problem, in my opinion, is the line:
output.write(conf.toString().getBytes());
which set the base64 in my Java call, and i don't understand/know how to do the same in my Postman call.
Try only adding the following values:
Then, add the content type and the values which you need to pass.
Trying to use HttpURLConnection to send HTTP chunked POST request.
conn.setRequestMethod("POST");
conn.setChunkedStreamingMode(16000);
conn.setRequestProperty("format", "InterleavedInt16");
conn.setRequestProperty("number-of-channels", "2");
conn.setRequestProperty("format", "InterleavedInt16");
conn.setRequestProperty("transfer-encoding", "chunked");
conn.setReadTimeout(12000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
have setup the conn as above. My question is do I need to manually breakdown the outputstream as to the chunk size. and call out.write() multiple times for each chunk. Or I can just call out.write() once but pass in the whole stream?
My current code to send the whole stream(assume size is 32000):
byte[] data = new byte[32000];
inputStream.read(data);
OutputStream out = conn.getOutputStream();
conn.connect();
out.write(data);
audioStream.close();
out.close();
This give me a "unexpected end of stream on Connection" error.
I am calling a SAP SOAP Service from a web servlet in Java. For some reason SAP is giving me an error every time I use special characters in the fields of my request such as 'è' or 'à'. The WSDL of the SOAP Service is defined in UTF-8 and I have set my character encoding accordingly as you can see below. However I am not sure this is the correct way. Also, notice that if I use SOAP UI (with the same envelope) the request works correctly so it must be something on Java side.
URL url = new URL(SOAP_URL);
String authorization = Base64Coder.encodeString(SOAP_USERNAME + ":" + SOAP_PASSWORD);
String envelope = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:soap:functions:mc-style'><soapenv:Header/><soapenv:Body><urn:ZwsMaintainTkt><item>à</item></urn:ZwsMaintainTkt></soapenv:Body></soapenv:Envelope>";
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(SOAP_TIMEOUT);
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
con.setRequestProperty("SOAPAction", SOAP_ACTION_ZWSMANTAINTKT);
con.setRequestProperty("Authorization", "Basic " + authorization);
con.setDoOutput(true);
con.setDoInput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(con.getOutputStream());
outputStreamWriter.write(envelope);
outputStreamWriter.close();
InputStream inputStream = con.getInputStream();
Since a soap-request is xml use the xml-header to specify the encoding of your request:
<?xml version="1.0" encoding="UTF-8"?>
new OutputStreamWriter(con.getOutputStream()) uses the platform-default encoding which most probably is some flavour of ISO8859. Use new OutputStreamWriter(con.getOutputStream(),"UTF-8") instead
We are sending HTTPURLRequest to server.
When we are sending English content its working fine.But, when we are sending Arabic language content we are getting
Server returned HTTP response code: 500
We had written below code
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(SendRequest.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
dataout.writeBytes(SendRequest);
dataout.flush();
dataout.close();
BufferedReader bufferreader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
When I use connection.getInputStream() I am getting 500 error
We are using utf-8 also.But, still getting the error
can any one help me
You can use a library to escape the special chars:
StringEscapeUtils.escapeJava("هولاء كومو")
This class is available on: Commons Lang from Apache
Hope this helps!
Check the HTTP Status Response Codes. An error happened on the server, so the diagnostics will need to be performed on the server, not on the client.
I am working on a java app, to upload images on yfrog.com.
I can post on the API page successfully but without binary files just with a string parameters.
Also the method I use only accept "String".
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.write(data); accept String only.
I tried to put the image path but it doesn't work.
Do yourself a favor and take a look at Apache HttpComponents.