本文的目的是記錄學(xué)習(xí)http過(guò)程遇到的問(wèn)題赚爵,因此使用tomcat搭建了服務(wù)器教沾,簡(jiǎn)單測(cè)試get和post在抛,本篇只是簡(jiǎn)單記錄并不深入探究get和post的差異褥伴,需要更詳細(xì)的可以看這篇http://www.reibang.com/p/78b7012e27b3
1.安裝tomcat與eclipse 腥沽。
這部分可以參考我這篇文章逮走。
tomcat+eclipse+servlet在chrome上實(shí)現(xiàn) https 雙向認(rèn)證簡(jiǎn)單記錄
2.新建工程。
點(diǎn)擊菜單欄File->New->Dynamic Web Project
隨意命名xxx今阳,點(diǎn)擊finish师溅。
然后茅信,選中剛剛新建的項(xiàng)目xxxx,右鍵選擇New->Servlet墓臭。
在Class name中填xxxx蘸鲸,然后點(diǎn)擊finish。
于是自動(dòng)生成如下文件窿锉。
代碼具體如下酌摇。
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test
*/
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Test() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
接下來(lái)將doPost()函數(shù)中的代碼修改一點(diǎn)。
將
doGet(request, response);
改為
response.getWriter().append("Served1111 at: ").append(request.getContextPath());
這樣在后續(xù)使用postman測(cè)試的get和post時(shí)候返回的數(shù)據(jù)就不一樣了嗡载。
改好后保存窑多,然后選中java文件后右鍵,選擇run as 洼滚,然后點(diǎn)擊run on server埂息。
點(diǎn)擊finish。
自動(dòng)訪問(wèn)下面的鏈接遥巴。
http://localhost:8080/Tttt/Test
response如下所示千康,這里是get。
3.postman簡(jiǎn)單使用
瀏覽器中只能測(cè)試get铲掐,無(wú)法測(cè)試post拾弃。早幾年windows中可以使用firefox中的poster或者chrome中的postman插件,但是現(xiàn)在不行了迹炼。
不過(guò)postman還能用砸彬,但是需要下載客戶端。
下載地址:
https://www.getpostman.com/downloads/
下載好所需版本后斯入,需要注冊(cè)賬號(hào)砂碉。
注冊(cè)號(hào)賬號(hào)后進(jìn)入客戶端,主界面如下刻两。這里不需要?jiǎng)?chuàng)建增蹭,直接x掉。
使用untitled request
先測(cè)試get磅摹。
方法選擇get滋迈,然后填上鏈接 http://localhost:8080/Tttt/Test,最后點(diǎn)擊send户誓。
結(jié)果如下饼灿。
將get改成post后再次send。
至此帝美,pc端的get和post的簡(jiǎn)單測(cè)試就完成了碍彭。
如果想使用android真機(jī)測(cè)試的話,在保證手機(jī)和電腦在同一局域網(wǎng)的情況下使用pc端的ip地址去替換localhost。
在cmd窗口中輸入ipconfig庇忌,找到ipv4地址舞箍。
然后android端可訪問(wèn)鏈接即為 http://xx.xx.xx.xx:8080/Tttt/Test (xx部分為本機(jī)ip地址)。
android端測(cè)試代碼比較簡(jiǎn)單皆疹,簡(jiǎn)單貼一下:
首先在manifest中添加網(wǎng)絡(luò)權(quán)限疏橄。
<uses-permission android:name="android.permission.INTERNET"/>
Activity文件如下。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button bt_get;
private Button bt_post;
//url需替換成自己的android端可訪問(wèn)的鏈接
private String url = "http://10.4.17.27:8080/Tttt/Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_get = (Button) findViewById(R.id.bt_get);
bt_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL httpUrl = new URL(url);
HttpURLConnection con=(HttpURLConnection) httpUrl.openConnection();
InputStream inputStrea = null;//字節(jié)流
inputStrea = con.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStrea);//轉(zhuǎn)為字符流
//通過(guò)bufferReader 讀取
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String content = null;
content = bufferedReader.readLine();
int responseCode = 0;//獲得狀態(tài)碼
responseCode = con.getResponseCode();
String headerField=con.getHeaderField("Server");//獲取消息頭 名字為 Server的頭
Log.d(TAG,"content = " + content);
Log.d(TAG, "responseCode = " + responseCode );
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
bt_post = (Button) findViewById(R.id.bt_post);
bt_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL httpUrl = new URL(url);
HttpURLConnection con=(HttpURLConnection) httpUrl.openConnection();
con.setDoOutput(true);//cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
//給服務(wù)器發(fā)送請(qǐng)求頭
con.setRequestMethod("POST");
con.getResponseCode();//表示 請(qǐng)求完成 一個(gè)請(qǐng)求是有來(lái)回的
InputStream inputStrea = null;//字節(jié)流
inputStrea = con.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStrea);//轉(zhuǎn)為字符流
//通過(guò)bufferReader 讀取
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String content = null;
content = bufferedReader.readLine();
int responseCode = 0;//獲得狀態(tài)碼
responseCode = con.getResponseCode();
String headerField=con.getHeaderField("Server");//獲取消息頭 名字為 Server的頭
Log.d(TAG,"content = " + content);
Log.d(TAG, "responseCode = " + responseCode );
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
xml文件如下略就。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/bt_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get"/>
<Button
android:id="@+id/bt_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="post"/>
</LinearLayout>
run后分別點(diǎn)擊get和post后結(jié)果如下捎迫。
09-16 16:00:50.822 28865-30737/? D/MainActivity: content = Served at: /Tttt
09-16 16:00:50.822 28865-30737/? D/MainActivity: responseCode = 200
09-16 16:00:52.274 28865-30746/? D/MainActivity: content = Served1111 at: /Tttt
09-16 16:00:52.274 28865-30746/? D/MainActivity: responseCode = 200
這篇文章到此就結(jié)束了,下面接著回到http的學(xué)習(xí)中去残制。
參考鏈接:
Postman安裝與使用
手機(jī)訪問(wèn)本地Tomcat服務(wù)器
http://www.reibang.com/p/de48dc7981fe
Java HttpURLConnection 小demo
http://www.reibang.com/p/863b1f2c4c74
GET與POST請(qǐng)求詳解
http://www.reibang.com/p/78b7012e27b3