Pregunta

Me gustaría volver a colocar una solicitud de publicación a una dirección determinada, que sea, por ejemplo,

http://staging.myproject.com/products.xml?productimaTitlefont>=titletest&productimacontentfont>=testContent&productimaPriceFont>=12.3&tags=aaaa.bbbb

Para las solicitudes de publicaciones, he creado un método universal:

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

donde los encabezados representan pares (clave, valor), por ejemplo ("producto [título], "titletest"). Traté de usar el método llamando a Postmetod ("http: //staging.myproject.com.products.xml", encabezados," xxx "); donde los encabezados incluían pares

("Producto [Título], "Titletest"),

("Producto [Contenido], "TestContent"),

(producto [precio], "12.3"),

("Etiquetas", "AAA, BBB")

Pero el servidor devolvió un mensaje de error.

¿Alguien sabe cómo analizar la dirección?

http://staging.myproject.com/products.xml?productimaTitlefont>=titletest&productimacontentfont>=testContent&productimaPriceFont>=12.3&tags=aaaa.bbbb

¿Para usarlo con el método anterior? ¿Qué parte es URL? ¿Están los parámetros establecidos correctamente?

Gracias.

¿Fue útil?

Solución

He encontrado 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

Parámetros:

("Producto [Título], "Titletest"),

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

(product[price], "12.3"),

("tags", "aaa,bbb")

Otros consejos

Parece ser parámetros de consulta de URL confuso, como el producto [precio] = 12.3 con encabezados de solicitud HTTP. El uso de SetRequestHeader () está destinado a establecer los encabezados de solicitud HTTP, que son meta-data asociados con cualquier solicitud HTTP.

Para establecer los parámetros de consulta, debe agregarlos a la URL, después de un "?" y urlencoded, como en su ejemplo de URL.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top