客户端通过http协议以多参数方式连接服务端
2015-03-05 21:52
295 查看
服务端:
/**
* 接收并处理http请求
*/
public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String broadcast = request.getParameter("broadcast");
String username = request.getParameter("username");
String title = request.getParameter("title");
String message = request.getParameter("message");
String uri = request.getParameter("uri");
String apiKey = Config.getString("apiKey", "");
logger.debug("apiKey=" + apiKey);
if (broadcast.equalsIgnoreCase("Y")) {
notificationManager.sendBroadcast(apiKey, title, message, uri);
} else {
notificationManager.sendNotifcationToUser(apiKey, username, title, message, uri);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
return null;
}
客户端:
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class ClientTest {
/**
* 第一种方案
* 多参数以post方式提交
* */
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{
StringBuilder sb = new StringBuilder();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}
byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据
//Content-Type: application/x-www-form-urlencoded
//Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return true;
}
return false;
}
/**
* 测试
* */
public void testSendPostRequest() throws Throwable{
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "smith赵虹荣");
params.put("timelength", "80");
new ClientTest().sendPostRequest("http://localhost:8080/msgServer.do", params, "UTF-8");
}
/**
* 第二种方案
* 多参数以post方式提交
* */
public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception{
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据
HttpPost post = new HttpPost(path); //form
post.setEntity(entitydata);
DefaultHttpClient client = new DefaultHttpClient(); //浏览器
HttpResponse response = client.execute(post);//执行请求
if(response.getStatusLine().getStatusCode()==200){
return true;
}
return false;
}
/**
* 测试
* */
public void testSendRequestFromHttpClient() throws Throwable{
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "赵虹荣");
params.put("timelength", "80");
boolean result = new ClientTest().sendRequestFromHttpClient("http://localhost:8080/msgServer.do", params, "UTF-8");
}
public static void main(String[] args) throws Exception{
try {
new ClientTest().testSendPostRequest();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/**
* 接收并处理http请求
*/
public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String broadcast = request.getParameter("broadcast");
String username = request.getParameter("username");
String title = request.getParameter("title");
String message = request.getParameter("message");
String uri = request.getParameter("uri");
String apiKey = Config.getString("apiKey", "");
logger.debug("apiKey=" + apiKey);
if (broadcast.equalsIgnoreCase("Y")) {
notificationManager.sendBroadcast(apiKey, title, message, uri);
} else {
notificationManager.sendNotifcationToUser(apiKey, username, title, message, uri);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
return null;
}
客户端:
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class ClientTest {
/**
* 第一种方案
* 多参数以post方式提交
* */
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{
StringBuilder sb = new StringBuilder();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}
byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据
//Content-Type: application/x-www-form-urlencoded
//Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return true;
}
return false;
}
/**
* 测试
* */
public void testSendPostRequest() throws Throwable{
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "smith赵虹荣");
params.put("timelength", "80");
new ClientTest().sendPostRequest("http://localhost:8080/msgServer.do", params, "UTF-8");
}
/**
* 第二种方案
* 多参数以post方式提交
* */
public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception{
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据
HttpPost post = new HttpPost(path); //form
post.setEntity(entitydata);
DefaultHttpClient client = new DefaultHttpClient(); //浏览器
HttpResponse response = client.execute(post);//执行请求
if(response.getStatusLine().getStatusCode()==200){
return true;
}
return false;
}
/**
* 测试
* */
public void testSendRequestFromHttpClient() throws Throwable{
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "赵虹荣");
params.put("timelength", "80");
boolean result = new ClientTest().sendRequestFromHttpClient("http://localhost:8080/msgServer.do", params, "UTF-8");
}
public static void main(String[] args) throws Exception{
try {
new ClientTest().testSendPostRequest();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
相关文章推荐
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互(转)
- 宋体请求客户端通过http和服务端进行交互
- 客户端与服务端交互之socket与http协议的比较
- Java客户端通过HttpURLConnection连接Web服务器的Session保存问题
- HTTP协议建立连接的方式
- android 三种通过Http 协议 提交数据方式
- Java客户端通过HttpURLConnection连接Web服务器的Session保存问题
- 关于netcat命令与http协议,客户端与服务端交互
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成
- 通过 http 协议上传文件(rfc1867协议概述,jsp 应用举例,客户端发送内容构造)
- 通过 http 协议上传文件(rfc1867协议概述,jsp 应用举例,客户端发送内容构造)
- winform通过HttpWebRequest(post方式)上传文件和传递参数
- xml应用(2): 通过向xsl传递参数+数据岛方式,实现在客户端单个xml的分框架显示
- android开发步步为营之4:客户端通过http和服务端进行交互