2011-12-20 58 views
0

我無法用put方法將數據發送到特定的鏈接。請找到code.Let下面我知道,如果我不得不做出任何改變無法使用HTTP PUT方法將數據發送到一個URL

public class test { 
    private static String url = "http://semldvw0728.google.net/.svc/web/testrequests/"; 
    private static String urlconnection; 

    public static void main(String[] args) throws IOException { 

    StringBuffer xmlString = new StringBuffer(); 

    xmlString.append("<TestRequest>"); 
    xmlString.append("<DateRequested>2011-12-20</DateRequested>"); 
    xmlString.append("<DemCapID>893467</DemCapID>"); 
    xmlString.append("<DemCapVersion>1</DemCapVersion>"); 
    xmlString.append("<IBIS_ID>13530</IBIS_ID>"); 
    xmlString.append("<ProjectName>LTS</ProjectName>"); 
    xmlString.append("<RequestedBy>ktmq331</RequestedBy>"); 
    xmlString.append("<SampleNumber>SN1033645061</SampleNumber>"); 
    xmlString.append("<Status>Sample Ordered</Status>"); 
    xmlString.append("</TestRequest>"); 

    System.out.println("xmlString :" + xmlString.toString()); 
    url = url + 893467; 
    System.out.println("URL : " + url); 

    try { 
     System.out.println("URL : " + url); 
     HttpClient client = new HttpClient(); 
     PutMethod putMethod = new PutMethod(url); 
     client.setConnectionTimeout(8000); 
     putMethod.setRequestBody(xmlString.toString()); 
     System.out.println("statusLine>>>" + putMethod.getStatusLine()); 
     System.out.println("statusLine>>>"+   putMethod.getResponseBodyAsString()); 
     putMethod.releaseConnection(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

更具體。添加更多信息。問題是什麼?有錯誤嗎? etc ... – 2011-12-20 20:36:41

回答

1

我想你需要撥打execute,或者你可以使用你創建的HttpClient對象call this

所以,也許加入這一行你設置請求體後您的putMethod ...

client.executMethod(putMethod); 
+0

你能否請我解釋一下如何創建HttpClient – Winz 2011-12-20 20:45:38

+0

我添加了如何給你提供的代碼打電話並將你鏈接到文檔。這應該足夠了。 – Dave 2011-12-20 20:46:13

+0

@ user1042983:'HttpClient client = new DefaultHttpClient();' – 2011-12-20 20:48:22