post请求客户端端代码:
1.
public void String LoginByPost(String username,String pwd){
String path = "";
URL url = new URL(paht);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);//请求超时
conn.setRequestMehtod("POST");
//准备数据
String data ="username=" + username +"&pwd=" + pwd;
conn.setRequestProperty("Content-Type","application/x-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length + "");
conn.setDoOutput(true); //是否允许向外写数据
OputStream os = conn.getOutputStream();
os.write(data.getByte());
int code = conn.getResponseCode();
if(code == 200){
InputStream is = conn.getInputStream();
String text = StreamTool.readInputStream(is);
return text;
}else{
return null;
}
}
2.
public void String LoginClientPost(String username, String pwd){
HttpClient client = new DefultHttpClient();
String path = "";
HttpPost = post = new HttpPost(path);
List<NameValuePair> paimer = new ArrayList<NameValuePair>();
paimer.add(new BasicNameValuePair("username",username));
post.setEntity(new UrlEncodedFormEntiry(paimer,"UTF-8"));
HttpResponse response = client.excute(post);
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = response.getEntity().getContent();
String text = StreamTool.readStream(is);
return text;
}else{
return null;
}
}