- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个 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/
我一直在研究一个调用 Rest API 的 portlet。当API被调用且请求的数据不存在时,返回相应的JSON格式的错误信息(带有Bad request http code - 400),如果id
我有这个在 Windows VM 上运行的简单代码: URL url = new URL("http:xxx.xxx.xxx.xxx/api/cities.json"); HttpURLConnect
我有一个这样的代码: HttpURLConnection connection = (HttpURLConnection) loginUrl.openConnection(); connection.
Google 提供了 2 个不同的 HttpURLConnection 用法示例。 调用InputStream的close http://developer.android.com/training/
我想对自己编写的HTTP Servlet发出POST请求。通过使用URL.openConnection()方法,好的情况(HTTP响应代码200)始终可以正常工作。但是当我收到所需的错误响应代码(例如
您好,我已经开始使用 HttpUrlConnection,我有一个关于 Http 请求实际发送时间的问题。 我在某处读到,调用 getInputStream() 时会发送实际请求。不过,我已经编写了一
从 android 创建 HTTPURLConnection 时,有人对何时关闭连接和何时断开连接有任何经验吗?是否应该始终使用断开连接,以便可以从池中重用连接而不是重新创建连接?使用断开连接与关闭有
问题: 尝试“连接”时出现间歇性但频繁的故障。 代码、调试消息和堆栈跟踪如下。我看过类似的 问题解答,以及 android 开发人员帮助,但找不到 对失败的回答。 任何帮助表示赞赏。 编码: publ
我一直在尝试创建一个小的应用程序,该应用程序连接到editText中的url并返回状态代码。 这是onCreate代码: override fun onCreate(savedInstanceSta
我正在尝试创建 get 连接,但是当我读取服务器上的 header 时,我发现请求方法接收为 POST 而不是 GET 最佳答案 尝试删除此行:connection.setDoOutput(true)
我正在尝试将 x-www-form-urlencoded 格式的正文发送到我的本地 Web API。我可以毫无问题地进行 GET 。这是我所做的: String urlParameters =
我是 Android 开发新手,在互联网上进行了一些咨询后,我想出了以下代码来连接到 Url、POST 字符串内容并读取其响应。 private static java.lang.String get
我使用 HTTPUrlConnection 和 GET 方法从服务器获取数据,返回字符串大约 13k 个字符,但我只收到 1228 个字符。 有什么解决方案可以超过接收所有字符吗? 这是我的代码: U
我正在使用 HttpUrlConnection 来使用 REST 服务。当我执行 GET 时,就像下面显示的那样,我不想获取逐个字符返回的信息。我想以返回的格式获得结果。例如,在这里,我想获得一个 b
我从互联网上找到了下面的两段代码,我正在我的应用程序中使用它。 我真的不明白的一件事是,为什么没有调用 HttpUrlConnection.connect() 来建立 Http 连接(握手),并且没有
当使用下面的代码发出 get 请求时: private String get(String inurl, Map headers, boolean followredirects) throws Ma
我向我的用户提供下载文件的机会。 有时这个文件非常大,我在下载时实现一个 ProgressDialog。 好吧,如果用户点击返回,我想取消AsyncTask并关闭连接: @Ov
我正在尝试使用 Java 建立与立交桥的服务器连接。但是,当我的请求参数中有空格(例如 Bulach West)时,我总是会收到错误的请求响应。我使用的代码如下: StringBuilder cont
我使用 HttpURLConnection 按顺序向服务器 HttpServer 发出多个短请求。我期望的最大吞吐量是多少?我无法获得超过 25 条记录/秒的数据。 我需要每秒至少 5000 条记录。
我使用以下连接来恢复下载: connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); co
我是一名优秀的程序员,十分优秀!