客户端通过http协议以多参数方式连接服务端
/**
* 接收并处理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();
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。