java - 发布方法 : How to make request to a given address?

标签 java apache-commons-httpclient

我想向给定地址重新发送 POST 请求,例如

http://staging.myproject.com/products.xml?product[title]=TitleTest&product[content]=TestContent&product[price]=12.3&tags=aaa,bbb

对于 POST 请求,我创建了一个通用方法:

private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Authorization", encodedAuthorizationString);
    if(headers != null && !headers.isEmpty()){
        for(Entry<String, String> entry : headers.entrySet()){
            post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
        }
    }
    client.executeMethod(post);
    String responseFromPost = post.getResponseBodyAsString();
    post.releaseConnection();
    return responseFromPost;
}

其中 header 表示对(键、值),例如(“product[title]”、“TitleTest”)。我尝试通过调用来使用该方法 postMethod("http://staging.myproject.com.products.xml ", headers, "xxx"); 其中 header 包含对

("product[title]", "TitleTest"),

("product[content]", "TestContent"),

(product[price], "12.3"),

("tags", "aaa,bbb")

但服务器返回错误消息。

有谁知道如何解析地址

http://staging.myproject.com/products.xml?product[title]=TitleTest&product[content]=TestContent&product[price]=12.3&tags=aaa,bbb

要按照上面的方法使用吗? url 是哪一部分?参数设置是否正确?

谢谢。

最佳答案

我发现一个问题:

private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Authorization", encodedAuthorizationString);
    if(headers != null && !headers.isEmpty()){
        for(Entry<String, String> entry : headers.entrySet()){
            //post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
            //in the old code parameters were set as headers (the line above is replaced with the line below)
            post.addParameter(new Header(entry.getKey(), entry.getValue()));
        }
    }
    client.executeMethod(post);
    String responseFromPost = post.getResponseBodyAsString();
    post.releaseConnection();
    return responseFromPost;
}

url = http://staging.myproject.com/products.xml

参数:

("product[title]", "TitleTest"),

("product[content]", "TestContent"),

(product[price], "12.3"),

("tags", "aaa,bbb")

关于java - 发布方法 : How to make request to a given address?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2027748/

相关文章:

java - 在 JEasyOpc 中请求订阅

java - 在 eclipse 之外的 tomcat 上部署轴 2 Web 服务

java - HTTP Apache,ssl 身份验证在 Java 1.8 中不起作用

java - 如何重用httpclient连接获取多个小文件

java - JMXBean 条目未显示

java - Spring Security - SPRING_SECURITY_LAST_EXCEPTION 的文档

java - 主要方法不会返回

android - SSL 证书 : "No peer certificate" 问题

java - Jersey-client 和 Apache HTTP Client 如何比较?

javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated in Tomcat with Apache HttpClient