【问题标题】:Multipart Request issue in android - Throwing BAD REQUESTandroid中的多部分请求问题 - 抛出错误的请求
【发布时间】:2013-05-15 11:54:53
【问题描述】:
private void CreateaMultiPartRequest() {
   try{
       HttpClient client = new DefaultHttpClient();  
       HttpPost post = new HttpPost("http://172.16.22.232:8080/RESTfulExample/rest/multiPartFileUpload");  
       MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
       post.setHeader("Content-type", "multipart/form-data; boundary="+"~~~~~");
       String Usersettings = "{\"uId\":\"atcc02\", \"bomId\":\"IC014654_12345\", \"fileName\":\"file_12345.pdf\"}";     
       File cfile = new File(receivedUri.getPath());
       reqEntity.addPart("file", new FileBody(cfile,"application/octet")); 
       reqEntity.addPart("settings", new StringBody(Usersettings,"application/json",Charset.forName("UTF-8")));
       post.setEntity(reqEntity); 
       HttpResponse response = client.execute(post);  
       HttpEntity resEntity = response.getEntity();  
            if (resEntity != null)  
                {
                  resEntity.consumeContent();  
                }
                else{                       
                }
            }           
            catch(Exception e)
            {
            }
        }

每当我尝试执行此代码时,我都会从服务器收到“400:BAD REQUEST”。无法在这里找出问题,有人可以帮助我纠正这个问题吗?

【问题讨论】:

  • 是的..我检查了同样的..事实上我也遵循了同样的方法。问题仍然存在
  • 此代码适用于 Android 吗? MultipartEntity 不是 Android 库的一部分。
  • 你在wireshark中检查你的请求了吗?也许这会有所帮助
  • 您发送的内容有误,根据 400 响应代码描述 The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications。 400 通常会在消息中返回原因是什么。你应该先检查一下...

标签: android multipart multipartentity


【解决方案1】:

您可能为 FileBody 使用了错误的 MIME 类型。改用 application/octet-stream。

要获得正确的 MIME 类型,我使用以下代码:

MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

看看我用来发出网络请求的小型库:http://code.google.com/p/android-dman/source/browse/trunk/src/org/nkuznetsov/lib/dman/DownloadManager.java

【讨论】:

    【解决方案2】:

    在这里你可以试试这个,它对我很好。

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
    
            FileBody bin = new FileBody(new File(args[5]));
            long size = bin.getContentLength();
    
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("image1", bin);
            String content = "";
            try {
                 httpPost.setEntity(reqEntity);
                    HttpResponse response = httpClient.execute(httpPost, localContext);
                    HttpEntity ent = response.getEntity();
                    InputStream st = ent.getContent();
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(st, writer);
                    content = writer.toString();                
    
            } catch (IOException e) {                   
    
            }
    

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 2016-09-25
      • 2017-02-10
      • 1970-01-01
      • 2018-01-28
      • 2014-05-21
      相关资源
      最近更新 更多