How to send an http mail request?

I want to send the zipped request body as an HTTP POST request for a web service based application. Can someone please help me, how can I send the http request requested by the client or how can I send the request body to the zip request as part of the POST-http request?

Edit : adding solution here

HttpURLConnection request = null; StringBuilder sb = new StringBuilder(getFileAsString("TestFile.txt")); String fileStr = getFileAsString("TestFile.txt"); HttpClient client = new HttpClient(); client.getState().setCredentials( new AuthScope(hostip, port), new UsernamePasswordCredentials("username", "password")); PutMethod post = new PutMethod(url); post.setRequestHeader("Content-Encoding", "gzip") 
+4
source share
3 answers

The HTTP protocol does not support compressed requests (it supports the exchange of compressed responses when a client announces its ability to process compressed content). If you want to implement compressed requests, then a protocol must be installed between the client and your web service so that the HTTP payload is always compressed so that the web service can always unpack and interpret the payload on the receiving side.

+5
source
 public static void main(String[] args) throws MessagingException, IOException { HttpURLConnection request = null; try { // Get the object of DataInputStream StringBuilder sb = new StringBuilder(getFileAsString("TestFile.txt")); String fileStr = getFileAsString("TestFile.txt"); System.out.println("FileData=" + sb); HttpClient client = new HttpClient(); client.getState().setCredentials( new AuthScope(hostip, portno), new UsernamePasswordCredentials(username, password)); PutMethod post = new PutMethod(url); post.setRequestHeader("Content-Encoding", "gzip"); post.setRequestHeader("Content-Type", "application/json"); post.setDoAuthentication(true); byte b[] = getZippedString(fileStr);; InputStream bais = new ByteArrayInputStream(b); post.setRequestBody(bais); try { int status = client.executeMethod(post); } finally { // release any connection resources used by the method post.releaseConnection(); } }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } 
0
source

I use a special servlet that decompresses and compresses requests and responses

 public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{ InputStream zipedStreamRequest = req.getInputStream(); String unzipJsonStr = ZipUtil.uncompressWrite(zipedStreamRequest); System.out.println("<---- ZIP request <----"); System.out.println(unzipJsonStr); MainHandler handler = new MainHandler(); String responseJson = handler.handle(unzipJsonStr); System.out.println("----> ZIP response ---->"); System.out.println(responseJson); OutputStream responseOutputStream = res.getOutputStream(); if (responseJson!=null) { ZipUtil.compressWrite(responseJson, responseOutputStream); } } 

here is my ziputil class

 public class ZipUtil { private static final int NB_BYTE_BLOCK = 1024; /** * compress and write in into out * @param in the stream to be ziped * @param out the stream where to write * @throws IOException if a read or write problem occurs */ private static void compressWrite(InputStream in, OutputStream out) throws IOException{ DeflaterOutputStream deflaterOutput = new DeflaterOutputStream(out); int nBytesRead = 1; byte[] cur = new byte[NB_BYTE_BLOCK]; while (nBytesRead>=0){ nBytesRead = in.read(cur); byte[] curResized; if (nBytesRead>0){ if (nBytesRead<NB_BYTE_BLOCK){ curResized = new byte[nBytesRead]; System.arraycopy(cur, 0, curResized, 0, nBytesRead); } else { curResized = cur; } deflaterOutput.write(curResized); } } deflaterOutput.close(); } /** * compress and write the string content into out * @param in a string, compatible with UTF8 encoding * @param out an output stream */ public static void compressWrite(String in, OutputStream out){ InputStream streamToZip = null; try { streamToZip = new ByteArrayInputStream(in.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { ZipUtil.compressWrite(streamToZip, out); } catch (IOException e) { e.printStackTrace(); } } /** * uncompress and write int into out * @param in * @param out * @throws IOException */ private static void uncompressWrite(InputStream in, OutputStream out) throws IOException{ InflaterInputStream inflaterInputStream = new InflaterInputStream(in); int nBytesRead = 1; byte[] cur = new byte[NB_BYTE_BLOCK]; while (nBytesRead>=0){ nBytesRead = inflaterInputStream.read(cur); byte[] curResized; if (nBytesRead>0){ if (0<=nBytesRead && nBytesRead<NB_BYTE_BLOCK){ curResized = new byte[nBytesRead]; System.arraycopy(cur, 0, curResized, 0, nBytesRead); } else { curResized = cur; } out.write(curResized); } } out.close(); } /** * uncompress and write in into a new string that is returned * @param in * @return the string represented the unziped input stream */ public static String uncompressWrite(InputStream in){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { uncompressWrite(in, bos); } catch (IOException e1) { e1.printStackTrace(); } try { byte[] byteArr = bos.toByteArray(); String out = new String(byteArr, "UTF-8"); return out; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } 

}

0
source

Source: https://habr.com/ru/post/1437561/


All Articles