带有HttpURLConnection的HTTP post文件

时间:2017-03-09 21:07:42

标签: android wordpress http http-post wordpress-rest-api

我试图做一个帖子文件,我很少被阻止,我可以通过curl做到但是这个代码我得到一个500 http错误代码,但如果我更改文件的文件名参数我得到400,所以它似乎帖子很好。端点是一个wordpress rest api,我正在使用Android模拟器Nexus 5 API 22.所以我不知道这是否正确或者是否有其他方法可以做我想做的事。

  try {
        FileInputStream fileInputStream = new FileInputStream( sourceFile );

        URL url = new URL("http://server/v2/media");

        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream( urlConnection.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; filename=\"" + imagePath + "\"" + lineEnd);
        dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
        dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        dos.writeBytes(lineEnd);


        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        Log.d(CameraAPI.TAG, "Bytes Available:" + bytesAvailable);
        Log.d(CameraAPI.TAG, "Buffer Size:" + bufferSize);

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);


        while ( bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"post\"" + lineEnd);
        dos.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        dos.writeBytes("Content-Length: " + id.length() + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(id);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"title\"" + lineEnd);
        dos.writeBytes("Content-Type: text/plain;charset=UTF-8"+lineEnd);
        dos.writeBytes("Content-Length: " + title.length() + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(title);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);


        // Execute HTTP Post Request
        Log.d(CameraAPI.TAG, "ResposeCode:" + urlConnection.getResponseCode());
        Log.d(CameraAPI.TAG, "Content-Length:" + urlConnection.getContentLength());
        Log.d(CameraAPI.TAG, "Content-Type:" + urlConnection.getContentType());
        Log.d(CameraAPI.TAG, "ResponseMessage:" + urlConnection.getResponseMessage());

        fileInputStream.close();
        dos.flush();
        dos.close();

        if ( urlConnection.getResponseCode() == 201 ) {
            InputStream bis = new BufferedInputStream(urlConnection.getInputStream());
            String result = convertStreamToString(bis);
            Log.d(CameraAPI.TAG, "DataInputStream:" + result);
            bis.close();
            return true;
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        return Boolean.valueOf(e.getMessage());
    } finally {
        urlConnection.disconnect();
    }

1 个答案:

答案 0 :(得分:0)

最后我得到了答案,我使用curl来查看请求是如何进行的..我改变了multipart / form-data进程:

  try {
            FileInputStream fileInputStream = new FileInputStream( sourceFile );

            URL url = new URL("http://server/wp/v2/media");

            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setUseCaches(false);
            urlConnection.setChunkedStreamingMode(0);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Content-Type", this.getMimeType(imagePath));
            urlConnection.setRequestProperty("Content-Disposition", "attachment;filename=\"" + sourceFile.getName() + "\";post=" + id + ";title=\"" + title + "\"" + lineEnd);

            DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            Log.d(CameraAPI.TAG, "Bytes Available:" + bytesAvailable);
            Log.d(CameraAPI.TAG, "Buffer Size:" + bufferSize);

            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            Log.d(CameraAPI.TAG, "Bytes to read:" + bytesRead);

            while ( bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }


            // Execute HTTP Post Request
            Log.d(CameraAPI.TAG, "ResposeCode:" + urlConnection.getResponseCode());
            Log.d(CameraAPI.TAG, "Content-Length:" + urlConnection.getContentLength());
            Log.d(CameraAPI.TAG, "Content-Type:" + urlConnection.getContentType());
            Log.d(CameraAPI.TAG, "ResponseMessage:" + urlConnection.getResponseMessage());

            fileInputStream.close();
            dos.flush();
            dos.close();

            if ( urlConnection.getResponseCode() == 201 ) {
                InputStream bis = new BufferedInputStream(urlConnection.getInputStream());
                String result = convertStreamToString(bis);
                Log.d(CameraAPI.TAG, "DataInputStream:" + result);
                bis.close();
                return true;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            return Boolean.valueOf(e.getMessage());
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }