使用搜狐Sendcloud的Webapi发送邮件:Jodd和Apache Httpclient

最近,在使用搜狐Sendcloud发邮件。
    Sendcloud提供http格式的webapi,方便地发送邮件,当然是要付费的。

   很早之前,http工具一直用Httpclient,后来觉得jodd更简单,就倾向于jodd的一些工具库了。

   使用jodd遇到一个问题:
  当邮件内容比较大时,比如1万多字符的时候,发送邮件失败。
Sendcloud服务器所在的Nginx,提示

414 Request-URI Too Large


<html>

<head><title>414 Request-URI Too Large</title></head>

<body bgcolor="white">

<center><h1>414 Request-URI Too Large</h1></center>

<hr><center>nginx</center>

</body>

</html>

  提交工单,与客服和技术支持,交流了几个小时,终于解决了问题。

第1种方法:使用官方给的Apache Httpclient的例子,发送邮件。
第2种方法:原来用Jodd,使用方式有问题。
Map<String, String> queryMap = new HashMap<String, String>();
		queryMap.put("api_user", API_USER);
		queryMap.put("api_key", API_KEY);
		queryMap.put("from", FROM);
		queryMap.put("to", to);
		queryMap.put("subject", subject);
		queryMap.put("html", html.substring(0,html.length()));
		HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
				.query(queryMap).send();
		String body = response.bodyText();



这个地方用的是“post” ,但是参数仍然放在了url后面,当数据量过大时,就有问题了。
正确的做法是: 
HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
.form(queryMap).send();
用form方法替代query方法。


有2个疑惑:
1.用post发送,为啥会把参数放在url后面?或者说,url后面接参数,还是post发送么?
2. jodd官方,有这句话:
    
Query parameters may be specified in the URL line (but then they have to be correctly encoded).
   
为啥是“可能”?

以下是一些代码 
 
Apache发送:
public static void send(String to, String subject, String html) {
		if (!check()) {
			return;
		}
		String url = URL;
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(url);

		List nvps = new ArrayList();
		nvps.add(new BasicNameValuePair("api_user", API_USER));
		nvps.add(new BasicNameValuePair("api_key", API_KEY));
		nvps.add(new BasicNameValuePair("from", FROM));
		nvps.add(new BasicNameValuePair("to", to));
		nvps.add(new BasicNameValuePair("subject", subject));
		nvps.add(new BasicNameValuePair("html", html));
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse response = httpclient.execute(httpost);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
				HttpEntity entity = response.getEntity();
				System.out.println(EntityUtils.toString(entity));
			} else {
				System.err.println("error");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}



Jodd发送:
public static void send(String to, String subject, String html) {
		if (!check()) {
			return;
		}
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("api_user", API_USER);
		queryMap.put("api_key", API_KEY);
		queryMap.put("from", FROM);
		queryMap.put("to", to);
		queryMap.put("subject", subject);
		queryMap.put("html", html.substring(0,html.length()));
		HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
				.form(queryMap).send();
		String body = response.bodyText();
		try {
			JSONObject jsonObject = JSONObject.parseObject(body);
			MailMessage msg = JSONObject.toJavaObject(jsonObject,
					MailMessage.class);

			String sendInfo = "to=" + to + ",subject=" + subject;
			if (msg.getMessage().equals(MailMessage.ERROR)) {
				logger.error("sendcloud,send mail failed:" + msg + ",sendInfo:"
						+ sendInfo);
			} else if (msg.getMessage().equals(MailMessage.SUCCESS)) {
				logger.info("sendcloud,send mail ok,sendInfo:" + sendInfo);
			}
		} catch (Exception e) {
			logger.error("send mail failed",e);
			logger.info(body);
		}

		// System.out.println(response);
	}


   jodd资料: http://jodd.org/doc/http.html

你可能感兴趣的:(httpclient,邮件,jodd,post,sendcloud)