无法使用Http put方法将数据发送到URL

时间:2011-12-20 20:32:12

标签: java

我无法使用put方法将数据发送到特定链接..请在下面找到代码。让我知道我是否需要进行任何更改

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();
    }
}

1 个答案:

答案 0 :(得分:1)

我认为你需要拨打execute,或者你可以使用你创建的HttpClient对象call this

因此,在为putMethod ...

设置请求主体后,可以添加此行
client.executMethod(putMethod);