Android 多部分 POST http 请求 415 错误
Android multipart POST http Request 415 Error
初学者 android 开发人员在这里。我正在尝试 POST 一个 .gpx 文件到我的网络数据库。但是,我收到了 HTTP 响应错误 415。我知道 415 表示内容类型不正确,而我尝试使用的 API 有此代码。感谢任何提示!
// Test that request has correct content type
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
这是我的 Http 请求 class
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
* @param requestURL
* @param charset
* @throws IOException
*/
public MultipartUtility(String requestURL, String charset)
throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Type", // content type multipart ( FILE)
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("Authorization", UserInfo.User.getToken());
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
}
/**
* Adds a form field to the request
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
.append(LINE_FEED);
//writer.append("Content-Type: text/plain; charset=" + charset).append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
/**
* Adds a upload file section to the request
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile)
throws IOException {
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append(
"Content-Disposition: form-data; name=\"" + fieldName
+ "\"; filename=\"" + fileName + "\"");
//Log.i("fileName",fileName);
writer.append("Content-Type: " + "multipart/form-data; boundary=" + boundary);
//.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
/**
* Adds a header field to the request.
* @param name - name of the header field
* @param value - value of the header field
*/
public void addHeaderField(String name, String value) {
writer.append(name + ": " + value).append(LINE_FEED);
writer.flush();
//Log.i("header",name + ": " + value);
}
/**
* Completes the request and receives response from the server.
* @return a list of Strings as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public List<String> finish() throws IOException {
List<String> response = new ArrayList<String>();
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}
这是我的activity
String requestURL = "url";
try {
MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addFilePart("filename", File);
List<String> response = multipart.finish();
System.out.println("SERVER REPLIED:");
for (String line : response) {
System.out.println(line);
}
} catch (IOException ex) {
System.err.println(ex);
}
}
这是我对 httpbin 的回复
/*I/System.out: 服务器回复:
I/System.out: {
I/System.out: "args": {},
I/System.out: "data": "",
I/System.out: "files": {
I/System.out: "FileName": "http://www.topografix.com/GPX/1/1\" version=\"1.0\">\n\n\r\n"
I/System.out: },
I/System.out: "form": {},
I/System.输出:"headers":{
I/System.out: "Accept-Encoding": "gzip",
I/System.out: "Authorization": "Bearer ",
I/System.out: "Content-Length": "322",
I/System.out: "Content-Type": "multipart/form-data;
边界====1488396069658===,
I/System.out: "Host": "httpbin.org",
I/System.out: "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 7.0; Android SDK built for x86 Build/NYC)"
I/System.out: },
I/System.输出:"json":空,
I/System.out: "origin": "50.174.210.222",
I/System.out: "url": "http://httpbin.org/post"
I/System.out: }*/
如果其他人遇到此问题..我使用 RETROFIT2 成功上传了我的文件。
//Retrofit2 method
public void uploadFile() {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClientBuilder.addInterceptor(interceptor);
okHttpClientBuilder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Request original = chain.request();
okhttp3.Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", UserInfo.User.getToken());
okhttp3.Request request = requestBuilder.build();
return chain.proceed(request);
}
});
MediaType MEDIA_TYPE_XML = MediaType.parse("application/xml");
RequestBody filePart = RequestBody.create(MEDIA_TYPE_XML, gpx);
MultipartBody.Part file = MultipartBody.Part.createFormData("Track", gpx.getName(), filePart);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("URL")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientBuilder.build());
Retrofit retrofit = builder.build();
UserClient client = retrofit.create(UserClient.class);
Call<ResponseBody> call = client.uploadFile(file);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Log.i("works", call.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i("Fail", "gg");
}
});
}
我的界面
public 界面用户客户端{
@Multipart
@POST("Gpx")
Call<ResponseBody> uploadFile(
@Part MultipartBody.Part File
);
}
public static <C> C createService(Class<C> cClass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(UrlManager.Manager.BASE_URL)
.build();
return retrofit.create(cClass);
}
此代码创建一个工厂,然后记录所有中间件请求。这段代码一旦调用,请求的所有内容都会自动记录在logcat中。您可以通过输入 okHTTP 进行过滤。
要使用拦截器:包括compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
初学者 android 开发人员在这里。我正在尝试 POST 一个 .gpx 文件到我的网络数据库。但是,我收到了 HTTP 响应错误 415。我知道 415 表示内容类型不正确,而我尝试使用的 API 有此代码。感谢任何提示!
// Test that request has correct content type
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
这是我的 Http 请求 class
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
* @param requestURL
* @param charset
* @throws IOException
*/
public MultipartUtility(String requestURL, String charset)
throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Type", // content type multipart ( FILE)
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("Authorization", UserInfo.User.getToken());
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
}
/**
* Adds a form field to the request
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
.append(LINE_FEED);
//writer.append("Content-Type: text/plain; charset=" + charset).append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
/**
* Adds a upload file section to the request
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile)
throws IOException {
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append(
"Content-Disposition: form-data; name=\"" + fieldName
+ "\"; filename=\"" + fileName + "\"");
//Log.i("fileName",fileName);
writer.append("Content-Type: " + "multipart/form-data; boundary=" + boundary);
//.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
/**
* Adds a header field to the request.
* @param name - name of the header field
* @param value - value of the header field
*/
public void addHeaderField(String name, String value) {
writer.append(name + ": " + value).append(LINE_FEED);
writer.flush();
//Log.i("header",name + ": " + value);
}
/**
* Completes the request and receives response from the server.
* @return a list of Strings as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public List<String> finish() throws IOException {
List<String> response = new ArrayList<String>();
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}
这是我的activity
String requestURL = "url";
try {
MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addFilePart("filename", File);
List<String> response = multipart.finish();
System.out.println("SERVER REPLIED:");
for (String line : response) {
System.out.println(line);
}
} catch (IOException ex) {
System.err.println(ex);
}
}
这是我对 httpbin 的回复
/*I/System.out: 服务器回复:
I/System.out: {
I/System.out: "args": {},
I/System.out: "data": "",
I/System.out: "files": {
I/System.out: "FileName": "http://www.topografix.com/GPX/1/1\" version=\"1.0\">\n\n\r\n"
I/System.out: },
I/System.out: "form": {},
I/System.输出:"headers":{
I/System.out: "Accept-Encoding": "gzip",
I/System.out: "Authorization": "Bearer ",
I/System.out: "Content-Length": "322",
I/System.out: "Content-Type": "multipart/form-data; 边界====1488396069658===,
I/System.out: "Host": "httpbin.org",
I/System.out: "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 7.0; Android SDK built for x86 Build/NYC)"
I/System.out: },
I/System.输出:"json":空,
I/System.out: "origin": "50.174.210.222",
I/System.out: "url": "http://httpbin.org/post"
I/System.out: }*/
如果其他人遇到此问题..我使用 RETROFIT2 成功上传了我的文件。
//Retrofit2 method
public void uploadFile() {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClientBuilder.addInterceptor(interceptor);
okHttpClientBuilder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Request original = chain.request();
okhttp3.Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", UserInfo.User.getToken());
okhttp3.Request request = requestBuilder.build();
return chain.proceed(request);
}
});
MediaType MEDIA_TYPE_XML = MediaType.parse("application/xml");
RequestBody filePart = RequestBody.create(MEDIA_TYPE_XML, gpx);
MultipartBody.Part file = MultipartBody.Part.createFormData("Track", gpx.getName(), filePart);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("URL")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientBuilder.build());
Retrofit retrofit = builder.build();
UserClient client = retrofit.create(UserClient.class);
Call<ResponseBody> call = client.uploadFile(file);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Log.i("works", call.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i("Fail", "gg");
}
});
}
我的界面
public 界面用户客户端{
@Multipart
@POST("Gpx")
Call<ResponseBody> uploadFile(
@Part MultipartBody.Part File
);
}
public static <C> C createService(Class<C> cClass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(UrlManager.Manager.BASE_URL)
.build();
return retrofit.create(cClass);
}
此代码创建一个工厂,然后记录所有中间件请求。这段代码一旦调用,请求的所有内容都会自动记录在logcat中。您可以通过输入 okHTTP 进行过滤。
要使用拦截器:包括compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'