From de1ac1f467cae449437d63d5c2a49e6736d56317 Mon Sep 17 00:00:00 2001 From: Pablo Fernandez Date: Sat, 7 May 2011 13:10:43 -0300 Subject: [PATCH] request charset handling --- src/main/java/org/scribe/model/Request.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/scribe/model/Request.java b/src/main/java/org/scribe/model/Request.java index 948e5c1..d49aa3e 100644 --- a/src/main/java/org/scribe/model/Request.java +++ b/src/main/java/org/scribe/model/Request.java @@ -2,6 +2,7 @@ package org.scribe.model; import java.io.*; import java.net.*; +import java.nio.charset.*; import java.util.*; import java.util.concurrent.TimeUnit; @@ -24,6 +25,7 @@ class Request private Map headers; private String payload = null; private HttpURLConnection connection; + private String charset; /** * Creates a new Http Request @@ -88,9 +90,11 @@ class Request void addBody(HttpURLConnection conn, String content) throws IOException { - conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes().length)); + if (this.charset == null) + this.charset = Charset.defaultCharset().name(); + conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes(charset).length)); conn.setDoOutput(true); - conn.getOutputStream().write(content.getBytes()); + conn.getOutputStream().write(content.getBytes(charset)); } /** @@ -246,7 +250,17 @@ class Request { this.connection.setReadTimeout((int) unit.toMillis(duration)); } - + + /** + * Set the charset of the body of the request + * + * @param charsetName name of the charset of the request + */ + public void setCharset(String charsetName) + { + this.charset = charsetName; + } + /* * We need this in order to stub the connection object for test cases */