import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TestHttpPost {
public static void main(String[] args) throws IOException {
//创建URL对象
URL url = new URL("http://127.0.0.1:8080/student/login.php");
//通过URL对象获取一个HttpURLConnection对象
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");//设置请求方式为POST
conn.setDoOutput(true);//设置允许输出数据到服务器
//设置请求头信息
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//获取输出流 通过输出流将POST请求提交的数据发送到服务器端
String data = "username=admin&password=admin";
//获取输出流对象 通过输出流将数据输出到服务器端
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());//数据写出 输出数据的操作 必须放置在读取数据前
//获取输入流 通过该流可以获取服务器端响应信息
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//判断响应状态码
if(conn.getResponseCode() == 200){
System.out.println("服务器请求处理成功,返回的响应:");
String msg = null;
while((msg=reader.readLine())!=null){
System.out.println(msg);
}
}
//断开连接
conn.disconnect();
}
}
##################################################
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText et_username;
- private EditText et_password;
- private Button bt_show;
- String result="";
- /**post请求URL地址*/
- private static final String URL="";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- bt_show.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- //android4.0后的新的特性,网络数据请求时不能放在主线程中。
- //9、使用线程执行访问服务器,获取返回信息后通知主线程更新UI或者提示信息。
- final Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if (msg.what == 1) {
- //8、提示读取结果
- Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
- }
- }
- };
- // 启动线程来执行任务
- new Thread() {
- public void run() {
- //请求网络
- loginByPost(et_username.getText().toString(), et_password.getText().toString());
- Message m = new Message();
- m.what = 1;
- // 发送消息到Handler
- handler.sendMessage(m);
- }
- }.start();
- }
- });
- }
- private void init(){
- et_username=(EditText)findViewById(R.id.et_username);
- et_password=(EditText)findViewById(R.id.et_password);
- bt_show=(Button)findViewById(R.id.bt_login);
- }
- /**
- * 请求后台,判断登录结果。
- * @param username
- * @param password
- */
- public void loginByPost(String username, String password) {
- try {
- //1、根据地址创建URL对象(网络访问的url)
- URL url = new URL(URL);
- //2、url.openConnection()打开网络链接
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- //3、设置请求的方式
- conn.setRequestMethod("POST");
- conn.setDoInput(true);//发送POST请求必须设置允许输出
- conn.setDoOutput(true);//发送POST请求必须设置允许输入
- //4、设置请求的头
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("Charset", "utf-8");
- String data = "username=" + URLEncoder.encode(username, "UTF-8")
- + "&password=" + URLEncoder.encode(password, "UTF-8");//传递的数据
- conn.setRequestProperty("Content-Length",String.valueOf(data.getBytes().length));
- //5、获取输出流
- OutputStream os = conn.getOutputStream();
- os.write(data.getBytes());
- os.flush();
- //6、获取响应的输入流对象
- InputStreamReader is = new InputStreamReader(conn.getInputStream());
- BufferedReader bufferedReader = new BufferedReader(is);
- StringBuffer strBuffer = new StringBuffer();
- String line = null;
- //7、读取服务器返回信息
- while ((line = bufferedReader.readLine()) != null) {
- strBuffer.append(line);
- }
- result = strBuffer.toString();
- //8、关闭InputStream、关闭http连接
- is.close();
- conn.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }