package org.nutz.http;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.nutz.http.Request.METHOD;
import org.nutz.lang.Encoding;
import org.nutz.lang.Lang;

public class Http {

	public static class multipart {
		public static String getBoundary(String contentType) {
			if (null == contentType)
				return null;
			int pos = contentType.indexOf(";");
			if (pos <= 10)
				return null;
			if (!contentType.substring(0, pos).equalsIgnoreCase("multipart/form-data"))
				return null;
			pos = contentType.indexOf("=", pos);
			if (pos < 0)
				return null;
			return contentType.substring(pos + 1);
		}

		public static String formatName(String name, String filename, String contentType) {
			StringBuilder sb = new StringBuilder();
			sb.append("Content-Disposition: form-data; name=\"");
			sb.append(name);
			sb.append("\"");
			if (null != filename)
				sb.append("; filename=\"" + filename + "\"");
			if (null != contentType)
				sb.append("\nContent-Type: " + contentType);
			sb.append('\n' + '\n');
			return sb.toString();
		}

		public static String formatName(String name) {
			return formatName(name, null, null);
		}
	}

	public static Response get(String url) {
		return Sender.create(Request.get(url)).send();
	}

	public static String post(String url, Map<String, Object> params, String inenc, String reenc) {
		return Sender.create(Request.create(url, METHOD.POST, params, null)).send().getContent();
	}

	public static String encode(Object s) {
		if (null == s)
			return "";
		try {
			// Fix issue 270, 临时按照意见来修改
			return new String(s.toString().getBytes(), Encoding.CHARSET_UTF8.name());
		}
		catch (UnsupportedEncodingException e) {
			throw Lang.wrapThrow(e);
		}
	}
}

@@ -65,16 +65,45 @@
 ption;%0D%0A
+import java.net.URLEncoder;%0D%0A
 import j
@@ -1651,47 +1651,79 @@
 %0A%09%09%09
-return new String(s.toString().getBytes
+// Fix issue 283, %E6%8C%89%E7%85%A7%E2%80%9C%E8%8C%B6%E5%87%A0%E2%80%9D%E7%9A%84%E6%84%8F%E8%A7%81%E5%86%8D%E6%AC%A1%E4%BF%AE%E6%94%B9%0D%0A%09%09%09return URLEncoder.encode(s.toString
 (), 

Fix issue 283, 按照“茶几”的意见再次修改