(HttpURLConnection)强制转化

时间:2023-03-09 03:26:35
(HttpURLConnection)强制转化

HTTP的请求详解在我的博客中已经讲解过:

http://blog.****.net/xiazdong/article/details/7215296

我在http://blog.****.net/xiazdong/article/details/7725867 中已经封装了一个HTTP请求的辅助类,因此可以很简单的发送GET、POST请求;

如HttpRequestUtil.sendGetRequest();是发送GET请求;

一、核心代码

HTTP GET 核心代码:

(1)String value = URLEncoder.encode(String value,"UTF-8");

(2)String path = "http://../path?key="+value;

(3)URL url = new URL(path);//此处的URL需要进行URL编码;

(4)HttpURLConnection con = (HttpURLConnection)url.openConnection();

(5)con.setRequestMethod("GET");

(6)con.setDoOutput(true);

(7)OutputStream out = con.getOutputStream();

(8)out.write(byte[]buf);

(9)int code = con.getResponseCode();

HTTP POST 核心代码:

(1)String value = URLEncoder.encode(String value,"UTF-8");

(2)byte[]buf = ("key="+value).getBytes("UTF-8");

(3)String path = "http://../path";

(4)URL url = new URL(path);//此处的URL需要进行URL编码;

(5)HttpURLConnection con = (HttpURLConnection)url.openConnection();

(6)con.setRequestMethod("POST");

(7)con.setDoOutput(true);

(8)OutputStream out = con.getOutputStream();

(9)out.write(byte[]buf);

(10)int code = con.getResponseCode();

二、GET和POST乱码解决方式

GET:

 

在doGet中加入:

String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

POST:

 

在doPost中加入:

 

request.setCharacterEncoding("UTF-8");

详情请看我的博文:

http://blog.****.net/xiazdong/article/details/7217022

三、服务器端代码

  1. package org.xiazdong.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. @WebServlet("/PrintServlet")
  9. public class PrintServlet extends HttpServlet {
  10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
  12. String age = new String(request.getParameter("age").getBytes("ISO-8859-1"),"UTF-8");
  13. System.out.println("姓名:"+name+"\n年龄:"+age);
  14. }
  15. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. request.setCharacterEncoding("UTF-8");
  17. System.out.println("姓名:"+request.getParameter("name")+"\n年龄:"+request.getParameter("age"));
  18. }
  19. }

(HttpURLConnection)强制转化

(HttpURLConnection)强制转化

四、Android端代码

在AndroidManifest.xml加入:

  1. <uses-permission android:name="android.permission.INTERNET"/>

MainActivity.java

  1. package org.xiazdong.network.submit;
  2. import java.io.OutputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.net.URLEncoder;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. public class MainActivity extends Activity {
  14. private EditText name, age;
  15. private Button getbutton, postbutton;
  16. private OnClickListener listener = new OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. try{
  20. if (getbutton == v) {
  21. /*
  22. * 因为是GET请求,所以需要将请求参数添加到URL后,并且还需要进行URL编码
  23. * URL = http://192.168.0.103:8080/Server/PrintServlet?name=%E6%88%91&age=20
  24. * 此处需要进行URL编码因为浏览器提交时自动进行URL编码
  25. * */
  26. StringBuilder buf = new StringBuilder("http://192.168.0.103:8080/Server/PrintServlet");
  27. buf.append("?");
  28. buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
  29. buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
  30. URL url = new URL(buf.toString());
  31. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  32. conn.setRequestMethod("GET");
  33. if(conn.getResponseCode()==200){
  34. Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
  35. }
  36. else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
  37. }
  38. if (postbutton == v) {
  39. /*
  40. * 如果是POST请求,则请求参数放在请求体中,
  41. * name=%E6%88%91&age=12
  42. *
  43. * */
  44. StringBuilder buf = new StringBuilder();
  45. buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
  46. buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
  47. byte[]data = buf.toString().getBytes("UTF-8");
  48. URL url = new URL("http://192.168.0.103:8080/Server/PrintServlet");
  49. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  50. conn.setRequestMethod("POST");
  51. conn.setDoOutput(true); //如果要输出,则必须加上此句
  52. OutputStream out = conn.getOutputStream();
  53. out.write(data);
  54. if(conn.getResponseCode()==200){
  55. Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
  56. }
  57. else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
  58. }
  59. }
  60. catch(Exception e){
  61. }
  62. }
  63. };
  64. @Override
  65. public void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.main);
  68. name = (EditText) this.findViewById(R.id.name);
  69. age = (EditText) this.findViewById(R.id.age);
  70. getbutton = (Button) this.findViewById(R.id.getbutton);
  71. postbutton = (Button) this.findViewById(R.id.postbutton);
  72. getbutton.setOnClickListener(listener);
  73. postbutton.setOnClickListener(listener);
  74. }
  75. }

(HttpURLConnection)强制转化