我想通过安全的restful web服务发送歌曲(mp3/wav)文件和一些数据.我正在使用MultipartEntity来发出HttpPost请求.但是当我通过HttpClient执行它时,服务器会回复此错误
HTTP状态400 - 错误请求类型:状态报告消息:错误请求客户端发送的请求在语法上不正确(错误请求).
但是,如果我们从Web界面调用它,那么该服务的效果非常好.请帮忙
它的代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost();
try {
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("email", new StringBody("test@testmail.com"));
reqEntity.addPart("password", new StringBody("123"));
reqEntity.addPart("title", new StringBody("My new song"));
reqEntity.addPart("musicData", new FileBody(new File(FilePath)));
// FIlePath is path to file and contains correct file location
postRequest.setEntity(reqEntity);
postRequest.setURI(new URI(ServiceURL));
HttpResponse response = httpclient.execute(postRequest);
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
Run Code Online (Sandbox Code Playgroud)
我还为MultipartEntity提供了apache-mime4j,httpclient,httpcore和httpmime jar.
这是服务的HTML页面捕捉.

这是我的代码.
我得到了Http 400错误,有人能帮帮我吗?
HttpClient httpClient;
HttpPost httpPost;
HttpResponse response;
HttpContext localContext;
FileEntity tmp = null;
String ret = null;
httpClient = new DefaultHttpClient( );
httpClient.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109) ;
httpPost = new HttpPost(url);
tmp = new FileEntity( data,"UTF-8" );
httpPost.setEntity( tmp );
httpPost.setHeader( "Content-Type", "multipart/form-data" );
httpPost.setHeader( "access_token", facebook.getAccessToken( ) );
httpPost.setHeader( "source", data.getAbsolutePath( ) );
httpPost.setHeader( "message", "Caption for the photo" );
localContext = new BasicHttpContext( );
response = httpClient.execute( httpPost,localContext );
Run Code Online (Sandbox Code Playgroud)
bobince,谢谢这是我的新ID,我会尝试将OAuth放到我的连接头.
这是我的旧代码,我会尽快更新.
private void uploadPicture( ) throws ParseException, …Run Code Online (Sandbox Code Playgroud) 我需要使用HttpPost方法上传带有多个参数的图像和视频文件,如文件名,描述,高度和宽度.
谢谢,建议赞赏.
根据此答案“ Android使用HTTP多部分表单数据将视频上传到远程服务器”,我执行了所有步骤。
但是我不知道如何为服务器端编码!我的意思是一个PHP简单的页面,可为我最忠实的敌人提供服务。
另一个问题是:YOUR_URL(以下代码段的第三行)必须是该PHP页面的地址吗?
private void uploadVideo(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a description of the video");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = …Run Code Online (Sandbox Code Playgroud) android ×5
http-post ×2
upload ×2
facebook ×1
file-upload ×1
httpclient ×1
java ×1
multipart ×1
php ×1