Androidはpostメソッドを使用してウェブサイトを要求する

4445 ワード

	//  Url,    ,      。
	public String readParse(String data) {
		
		String str="";
		//synUrl      URL
		HttpPost httpRequest = new HttpPost(synUrl);
	
		try {
						
	        HttpClient client = new DefaultHttpClient();
	        //     
	        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
	        //     
	        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
	        
	        //       
	        data=URLEncoder.encode(data,"UTF-8");
	        
	        //      string entity  ,       
			StringEntity strEntity=new StringEntity(data);
			strEntity.setContentEncoding("UTF-8");
			
			//         http    
			httpRequest.setEntity(strEntity);
			
			//      
			httpRequest.setHeader("Content_Type","application/json;charset=UTF-8");
			
			//    ,      
			HttpResponse httpResponse =client.execute(httpRequest);
		    httpResponse.setHeader("content_type","application/json;charset=UTF-8");
		    
			//       200,    
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				//       
				str=EntityUtils.toString(httpResponse.getEntity()).trim(); 
				strResult = str;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return strResult;
	}

最近,学習を用いてandroidを開発し,androidエンドコードを与えるフロントバックグラウンド通信符号化の問題に関連した.
 
ネット上にはもう一つの方法があります.
  
   
public boolean post(String username, String password) throws Exception {
username = URLEncoder.encode(username);//         URL  
password = URLEncoder.encode(password);
String params = "username=" + username + "&password=" + password; 
byte[] data = params.getBytes();

URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
//       POST
conn.setRequestMethod("POST");
//  post        
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//    ,     
conn.setRequestProperty("Content-Length", data.length + "");//        ,       

conn.setDoOutput(true);//     
conn.getOutputStream().write(data);//     

return conn.getResponseCode() == 200;
}

 
 
異なる2つのテキスト転送フォーマットが使用されていますが、いずれもURLEncoderクラスが使用されています.このクラスは符号化に使用されています.漢字は転送中にバイトが失われるため、この問題を解決するために再符号化する必要があります.
次は符号化の実験です.
   
public class test {

	
	public static void main(String args[])
	{
		 try {
		 
         String str=" ";
         //    
		 String res=URLEncoder.encode(str,"UTF-8");
		 System.out.println("encode one:"+res);
		 //     
		 res=URLEncoder.encode(res,"UTF-8");         
		 System.out.println("encode two:"+res);
		 
		 //    
		 res=URLDecoder.decode(res,"UTF-8");
		 System.out.println("dencode one:"+res);
		 //    
		 res=URLDecoder.decode(res,"UTF-8");
		 System.out.println("dencode two:"+res);
		 
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * encode  one:%E4%BD%A0
	 * encode  two:%25E4%25BD%25A0
	 * dencode one:%E4%BD%A0
	 * dencode two: 
	*/
}

 
 
実験によりencode two:従来のベースで%25を増やしただけで,符号化された伝送を経てサーバ側にURLDecoderクラスを介して復号できることが分かったので,乱符号化の問題は起こらなかった.
符号化されていない場合、「あなた」の字を送信するパケットは以下の通りです.
 
  
POST /Todo/admin/syn.action HTTP/1.1

content_type: application/json;charset=UTF-8

Content-Length: 320

Content-Type: application/json;charset=UTF-8

Content-Encoding: UTF-8

Host: 10.16.15.88:9091

Connection: Keep-Alive

User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)



{"title":"?"}