2012-07-10 213 views
2

我可以在SOAP UI中執行POST請求。但我不能從java做同樣的事情,嘗試了大約5-6個小時,並且找不到優勝組合。這裏是我的文章中,我試圖讓工作XSD架構部分:構建http POST請求的問題

<method name="POST"> 
<request> 
<param name="username" style="query" type="xs:string"/> 
<param name="id" style="query" type="xs:long"/> 
<representation mediaType="multipart/form-data"/> 
</request> 

肥皂UI我只是C/P有效載荷:

<?xml version="1.0" encoding="UTF-8"?> 
<ImageList xmlns="http://someurl/1.0/image" > 
    <Image> 
    <Name>sampler.jpg</Name> 
    <Filename>C:\\sampler.jpg</Filename> 
    <Label> 
     <Value>Test image</Value> 
    </Label> 
    <ImageMetadata> 
     <Format>jpg</Format> 
     <Height>300</Height> 
     <Width>400</Width> 
    </ImageMetadata> 
    </Image> 
</ImageList> 

然後我在附件選項卡中添加附件。 NameContent-Type,等我得到有效的響應代碼,但是我不設法用java做的一樣,這裏是我的了:

HttpRequestBase post = new HttpPost(); 
    HttpClient client = new DefaultHttpClient(); 
    try { 
     post.setURI(new URI(URL)); 
    } catch (URISyntaxException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    // set number of retries 
    post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); 
    HttpResponse response = null; 
    HttpPost post = (HttpPost) method; 
    try { 
    post.setURI(new URI(URL)); 
    } catch (URISyntaxException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
    } 

    FileBody uploadFilePart = new FileBody(new File("C:\\sampler.jpg")); 
    MultipartEntity reqEntity = new MultipartEntity(); 
    reqEntity.addPart("upload-file", uploadFilePart); 

    //payload 
    String requestBody = fileToString("src/main/resources/imageBodyPayload.xml"); 

    HttpParams parameters = new BasicHttpParams(); 
    parameters.setLongParameter("id", 951); 
    parameters.setParameter("username", "test"); 

    post.setParams(parameters); 
    post.setEntity(new StringEntity(requestBody, "multipart/form-data", HTTP.UTF_8)); 
    response = client.execute(post); 

所以我設定的有效載荷爲字符串POST請求,但我不能同時添加文件附件。

這是肥皂UI原始請求的樣子:

POST http://localhost:9080/imageUpload/?id=951&userame=test HTTP/1.1 
Accept-Encoding: gzip,deflate 
Content-Type: multipart/form-data; boundary="----=_Part_9_23652504.1341953390382" 
MIME-Version: 1.0 

無法複製同樣的行爲

+1

也許值得與任何HTTP框架,這是標記? – davidfrancis 2012-07-10 22:06:47

回答

1

你而不安裝MultipartEntity在您的示例HttpPost的要求,這是爲什麼該文件未上傳。也許嘗試是這樣的:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 

MultipartEntity reqEntity = new MultipartEntity(); 

String requestBody = fileToString("src/main/resources/imageBodyPayload.xml"); 
reqEntity.addPart("request-body", new StringBody(requestBody)); 

FileBody fileBody = new FileBody(new File("C:\\sampler.jpg")); 
reqEntity.addPart("upload-file", fileBody); 

HttpParams parameters = new BasicHttpParams(); 
parameters.setLongParameter("id", 951); 
parameters.setParameter("username", "test"); 

httppost.setParams(parameters); 
httppost.setEntity(reqEntity); 

HttpResponse response = httpclient.execute(httppost); 

我不知道「請求體」的價值將是你的情況是什麼,但一個多/表單數據請求的每個部分都需要有一個名字,所以如果您嘗試在單個請求中發送文件和一些XML,則需要同時提供文件部分和XML部分名稱。

「multipart/form-data」消息包含一系列部分,每個部分代表一個成功的控件。 每個零件都應包含一個名稱屬性,用於指定相應控件的控件名稱。

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2