JAVA HttpsConnection POST发送图像和文本

时间:2015-09-05 15:12:25

标签: java image post http-status-code-400 telegram-bot

我试图通过HTTP POST发送文本和图像,我总是得到400错误。我想用它来通过Telegram bot发送一个图像,我正在使用这个答案中提供的代码,文本和咬合参数https://stackoverflow.com/a/2793153/1970613进行了少量修改。

可能是什么错误?非常感谢先进!

                    String param  = "chat_id=mi_id&photo=";
                    String charset = "UTF-8"; 
                    String request = "https://api.telegram.org/bot-botCOde/sendPhoto";
                    URL url = new URL( request );
                    File binaryFile = new File("/home/joseantonio/imagen.jpg");
                    String CRLF = "\r\n"; // Line separator required by multipart/form-data.
                    String boundary = Long.toHexString(System.currentTimeMillis());

                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
                    conn.setDoOutput( true );
                    conn.setInstanceFollowRedirects( false );
                    conn.setRequestMethod( "POST" );
                    conn.setRequestProperty( "charset", "utf-8");
                    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                        conn.setUseCaches( false );
                    try{ 
                         OutputStream output = conn.getOutputStream();
                         PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);

                         writer.append("--" + boundary).append(CRLF);
                         writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
                         writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
                         writer.append(CRLF).append(param).append(CRLF).flush();

                         writer.append("--" + boundary).append(CRLF);
                         writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
                         writer.append("Content-Type: image/jpg").append(CRLF);
                         writer.append("Content-Transfer-Encoding: binary").append(CRLF);
                         writer.append(CRLF).flush();                            
                         Files.copy(binaryFile.toPath(), output);
                         output.flush(); // Important before continuing with writer!
                         writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

                         // End of multipart/form-data.
                         writer.append("--" + boundary + "--").append(CRLF).flush();

                        int responseCode2 = conn.getResponseCode();

                        System.out.println("\nSending 'GET' request to URL : " + request);
                        System.out.println("Response Code : " + responseCode2);

1 个答案:

答案 0 :(得分:0)

我相信您使用https网址的原因是您遇到错误的原因。当您提交https网址时,您需要处理稍微不同的事情。查看示例here