日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(HttpURLConnection)强制转化

發布時間:2024/4/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (HttpURLConnection)强制转化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

HTTP的請求詳解在我的博客中已經講解過:

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

?

我在http://blog.csdn.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.csdn.net/xiazdong/article/details/7217022

?

三、服務器端代碼

?

[java]?view plaincopy
  • package?org.xiazdong.servlet;??
  • ??
  • 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;??
  • ??
  • @WebServlet("/PrintServlet")??
  • public?class?PrintServlet?extends?HttpServlet?{??
  • ??
  • ????protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{??
  • ????????String?name?=?new?String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");??
  • ????????String?age?=?new?String(request.getParameter("age").getBytes("ISO-8859-1"),"UTF-8");??
  • ????????System.out.println("姓名:"+name+"\n年齡:"+age);??
  • ????}??
  • ??
  • ????protected?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{??
  • ????????request.setCharacterEncoding("UTF-8");??
  • ????????System.out.println("姓名:"+request.getParameter("name")+"\n年齡:"+request.getParameter("age"));??
  • ????}??
  • }??

  • ?

    ?

    四、Android端代碼

    ?

    在AndroidManifest.xml加入:

    ?

    [html]?view plaincopy
  • <uses-permission?android:name="android.permission.INTERNET"/>??

  • MainActivity.java

    ?

    ?

    [java]?view plaincopy
  • package?org.xiazdong.network.submit;??
  • ??
  • 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.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?name,?age;??
  • ????private?Button?getbutton,?postbutton;??
  • ????private?OnClickListener?listener?=?new?OnClickListener()?{??
  • ????????@Override??
  • ????????public?void?onClick(View?v)?{??
  • ????????????try{??
  • ????????????????if?(getbutton?==?v)?{??
  • ????????????????????/*?
  • ?????????????????????*?因為是GET請求,所以需要將請求參數添加到URL后,并且還需要進行URL編碼?
  • ?????????????????????*?URL?=?http://192.168.0.103:8080/Server/PrintServlet?name=%E6%88%91&age=20?
  • ?????????????????????*?此處需要進行URL編碼因為瀏覽器提交時自動進行URL編碼?
  • ?????????????????????*?*/??
  • ????????????????????StringBuilder?buf?=?new?StringBuilder("http://192.168.0.103:8080/Server/PrintServlet");??
  • ????????????????????buf.append("?");??
  • ????????????????????buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");??
  • ????????????????????buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));??
  • ????????????????????URL?url?=?new?URL(buf.toString());??
  • ????????????????????HttpURLConnection?conn?=?(HttpURLConnection)url.openConnection();??
  • ????????????????????conn.setRequestMethod("GET");??
  • ????????????????????if(conn.getResponseCode()==200){??
  • ????????????????????????Toast.makeText(MainActivity.this,?"GET提交成功",?Toast.LENGTH_SHORT).show();??
  • ????????????????????}??
  • ????????????????????else?Toast.makeText(MainActivity.this,?"GET提交失敗",?Toast.LENGTH_SHORT).show();??
  • ????????????????}??
  • ????????????????if?(postbutton?==?v)?{??
  • ????????????????????/*?
  • ?????????????????????*?如果是POST請求,則請求參數放在請求體中,?
  • ?????????????????????*?name=%E6%88%91&age=12?
  • ?????????????????????*??
  • ?????????????????????*?*/??
  • ????????????????????StringBuilder?buf?=?new?StringBuilder();??
  • ????????????????????buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");??
  • ????????????????????buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));??
  • ????????????????????byte[]data?=?buf.toString().getBytes("UTF-8");??
  • ????????????????????URL?url?=?new?URL("http://192.168.0.103:8080/Server/PrintServlet");??
  • ????????????????????HttpURLConnection?conn?=?(HttpURLConnection)url.openConnection();??
  • ????????????????????conn.setRequestMethod("POST");??
  • ????????????????????conn.setDoOutput(true);?//如果要輸出,則必須加上此句??
  • ????????????????????OutputStream?out?=?conn.getOutputStream();??
  • ????????????????????out.write(data);??
  • ????????????????????if(conn.getResponseCode()==200){??
  • ????????????????????????Toast.makeText(MainActivity.this,?"GET提交成功",?Toast.LENGTH_SHORT).show();??
  • ????????????????????}??
  • ????????????????????else?Toast.makeText(MainActivity.this,?"GET提交失敗",?Toast.LENGTH_SHORT).show();??
  • ????????????????}??
  • ????????????}??
  • ????????????catch(Exception?e){??
  • ??????????????????
  • ????????????}??
  • ????????}??
  • ????};??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ????????name?=?(EditText)?this.findViewById(R.id.name);??
  • ????????age?=?(EditText)?this.findViewById(R.id.age);??
  • ????????getbutton?=?(Button)?this.findViewById(R.id.getbutton);??
  • ????????postbutton?=?(Button)?this.findViewById(R.id.postbutton);??
  • ????????getbutton.setOnClickListener(listener);??
  • ????????postbutton.setOnClickListener(listener);??
  • ????}??
  • }??

  • 總結

    以上是生活随笔為你收集整理的(HttpURLConnection)强制转化的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。