多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 客戶端通過http協議以多參數方式連接服務端

客戶端通過http協議以多參數方式連接服務端

來源:程序員人生   發布時間:2015-03-20 08:48:40 閱讀次數:3246次
服務端:
/**
 * 接收并處理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 {
/**
* 第1種方案
* 多參數以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()⑴);  
        }  
        byte[] entitydata = sb.toString().getBytes();//得到實體的2進制數據  
        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⑻");  
    }

/**
* 第2種方案
* 多參數以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⑻");  
}    

public static void main(String[] args) throws Exception{
try {
new ClientTest().testSendPostRequest();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产精品日韩欧美一区二区三区 | 毛片毛片毛是个毛毛片 | 国产日本欧美在线观看乱码 | 欧美日本在线一区二区三区 | 欧美一区二区三区视频在线观看 | 国产精品久久久久影视不卡 | 久久亚洲精品中文字幕二区 | 最近免费中文在线视频 | 免费看一区二区三区 | 性欧美极品xxxx欧美一区二区 | 国产69精品久久久久99 | 中文字幕一区二区三区乱码aⅴ | 日本护士xxxxxx. | 亚洲综合在线视频 | 亚洲欧美日韩另类小说 | 最新国产福利片在线观看 | 中文有码 | freexx欧美| 国内精品哆啪啪 | 国产亚洲人成在线影院 | v天堂| 成人欧美精品久久久久影院 | 欧美一区2区| 久久久久久久久国产 | 国产成人精品一区二三区2022 | 逼逼片| 亚洲免费色视频 | 羞羞的动漫网站 | 欧美日韩在线观看一区 | 免费看啪啪网站 | 最近中文字幕免费6 | 最近中文字幕国语免费完整 | 视频在线高清完整免费观看 | 国产精品区一区二区三 | 亚洲综合五月 | 欧美一区二区视频 | 成人爱爱网站在线观看 | 亚洲欧美韩国日本 | 国产在线拍国产拍拍偷 | 亚洲精品国产三级在线观看 | h在线免费 |