java net.URL api 发送http消息

package gov.hn12396.appintegration.mule.mail.client;

import gov.hn12396.appintegration.mule.util.EncoderUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
/**
 * 模拟浏览器通过发送http请求,调用应用集成平台的mail协议,发送电子邮件
 * @author liuxp
 *
 */
public class MailTestClient {
    /**
     * 保存应用集成平台暴露给其他应用程序调用的地址
     */
//	private static String Mail_URI = "http://localhost:8082/email/?";
	private static String Mail_URI = "http://10.2.11.40:8082/email/?";

	/**
	 * 读取http请求的信息
	 * @throws IOException
	 */
	public static void readHttpFromMail() throws IOException {
		StringBuffer bufUrl = new StringBuffer(Mail_URI);
		bufUrl.append("[email protected]&[email protected]&subject=dd&text=998打算发放");
		URL url = new URL(bufUrl.toString()); // 设置请求的链接
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//		System.out.println(conn.getResponseCode()); // 查看响应状态码
//		System.out.println(conn.getHeaderField("Content-Length")); // 响应文本内容的长度
//		System.out.println(conn.getContentEncoding()); // 响应文本内容的编码
		InputStream in = conn.getInputStream(); // 获取一个和服务器返回的内容相关联的流
		try {
			int len = 0;
			byte[] buffer = new byte[1024];
			while ((len = in.read(buffer)) > 0) {
				System.out.println(new String(buffer, 0, len)); // 输出到控制台
			}
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
	public static void main(String[] args) throws IOException {
		readHttpFromMail();
//		sendHttpToJDBC();StringToEmailMessage
	}
}

你可能感兴趣的:(java net.URL api 发送http消息)