2013-11-05 110 views
1

如何提取一個特定的HTTP報頭的值(例如:設置Cookie:AUTH = XYZABC)從由下面的代碼接收到的HTTP 200 OK響應 -的Android解析HTTP POST響應

public void sendPost() { 
HttpClient httpClient = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://172.26.207.10/myexample/webauth/auth_default_submit"); 

// Set User-Agent and any other headers you want 
// Note: you don't have to set most of the headers manually, for example Content-Type and Content-Length 
post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0"); 
post.setHeader("Referer", "http://xxx.xxx.xxx.xx/myexample/webauth/auth_default?app_name=SampleWebAppTesting"); 

// Add the parameters 
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
parameters.add(new BasicNameValuePair("pincode", "9381")); 
parameters.add(new BasicNameValuePair("response", "allow")); 
parameters.add(new BasicNameValuePair("token", "dc7553b7d7910e51e331d5c3245133ba90b135da1504299510e91984f4e2cf81")); 
post.setEntity(new UrlEncodedFormEntity(parameters)); 

// Do the actual post 
HttpResponse response = httpClient.execute(post); 

// Read the response 
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
String line; 
StringBuffer responseBuffer = new StringBuffer(); 
while ((line = rd.readLine()) != null) { 
    responseBuffer.append(line); 
} 
rd.close(); 

// Print the results 
System.out.println(responseText.toString()); 

}

回答