gpt4 book ai didi

android - 带有 HttpURLConnection 的 HTTP post 文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:25:21 24 4
gpt4 key购买 nike

我正在尝试创建一个 post 文件,但几乎没有被阻止,我可以通过 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();
}

最佳答案

最后我得到了答案,我使用 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();
}
}

关于android - 带有 HttpURLConnection 的 HTTP post 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42705418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com