并发测试程序

package com.thread.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class TaskThread implements Runnable {

	@Override
	public void run() {
		
		System.out.println(Thread.currentThread().getName());
		try {
			URL url = new URL("http://127.0.0.1:8080/emp");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setReadTimeout(3000);
			connection.setDoOutput(true);
			OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
//			out.write("loginUsername=sysadmin&password=48A365B4CE1E322A55AE9017F3DAF0C0&lang=zh_CN"); // post的关键所在!

			out.flush();
			out.close();

			// 一旦发送成功,用以下方法就可以得到服务器的回应:
			String sCurrentLine;
			String sTotalString;
			sCurrentLine = "";
			sTotalString = "";
			InputStream l_urlStream;
			l_urlStream = connection.getInputStream();
			// 传说中的三层包装阿!
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString += sCurrentLine + "\r\n";

			}
//			System.out.println(sTotalString);
			System.out.println("end.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(java)