Domanda

Vorrei recuperare una richiesta post a un determinato indirizzo, ad esempio lasciarlo

http://staging.myproject.com/products.xml?product sultle lasting=titletest&productContent lasting=testContent&productoroprice lasting=12.3&tags=aaa,bbb

Per le richieste di post ho creato un metodo universale:

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

dove le intestazioni rappresentano coppie (chiave, valore), ad esempio ("prodotto [titolo], "titletest"). Ho provato a usare il metodo chiamando PostMethod ("http: //staging.myproject.com.products.xml", intestazioni," xxx "); dove le intestazioni includevano coppie

("Prodotto [titolo], "titletest"),

("Product [Content], "TestContent"),

(Prodotto [Price], "12.3"),

("Tags", "AAA, BBB")

Ma il server ha restituito un messaggio di errore.

Qualcuno sa come analizzare l'indirizzo

http://staging.myproject.com/products.xml?product sultle lasting=titletest&productContent lasting=testContent&productoroprice lasting=12.3&tags=aaa,bbb

Per usarlo con il metodo sopra? Quale parte è URL? I parametri sono impostati correttamente?

Grazie.

È stato utile?

Soluzione

Ho trovato un problema:

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

Parametri:

("Prodotto [titolo], "titletest"),

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

(product[price], "12.3"),

("tags", "aaa,bbb")

Altri suggerimenti

Sembra che tu stia confondendo i parametri di query URL, come il prodotto [prezzo] = 12,3 con le intestazioni di richiesta HTTP. L'utilizzo di SetrequestHeader () ha lo scopo di impostare le intestazioni della richiesta HTTP, che sono metadati associati a qualsiasi richiesta HTTP.

Per impostare i parametri delle query, dovresti aggiungerli all'URL, dopo un '?' e urlencoded, come nel tuo esempio URL.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top