(HttpURLConnection)强制转化
2016-07-15 11:31
489 查看
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 plain copypackage 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 plain copy
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity.java
[java] view plain copy
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);
}
}
相关文章推荐
- apache2.2配置httpd.conf
- Retrofit2的基本用法
- java网络socket编程(五)之Socket扩展2--实现重定向服务器
- 神经网络公式推导
- stomp 连接错误: Whoops! Lost connection to http://localhost:8080/spring13/stomp 的解决方法
- android异步任务处理(网络等耗时操作)
- VMware中linux系统的网络设置
- 所有HTTP请求参数及报文查看SERVLET【原】
- zimbra邮件系统安装SSL(HTTPS)方法
- VS2010MFC之网络编程——添加链接器
- 使用httpclient与第三方公司对接传递数据NameValuePair的用法
- 网络流与线性规划24题 之 餐巾计划问题
- 网络流与线性规划24题 之 餐巾计划问题
- Android 判断用户2G/3G/4G移动数据网络
- udp和tcp
- 嵌入式设备 以及手机 最新判断网络是否连接成功的最好方法,你值得知道
- 使用TCP/IP的套接字(Socket)进行通信
- CentOS6.7上搭建httpd-2.2
- http请求和响应格式
- TCP编程(python)