//说明:此处需引用httpclient、httpcore、commons-logging三个jar包
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;import java.util.*;importjava.security.MessageDigest;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.message.BasicNameValuePair;import org.apache.http.*;importjavax.crypto.SecretKey;importjavax.crypto.spec.DESKeySpec;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.SecretKeyFactory;importjavax.crypto.Cipher;public static voidmain(String[] args) {
String url="http://xxx.com/api/MsgSend.asmx/SendMes";
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("userCode", "用户名"));
nvps.add(new BasicNameValuePair("userPass", "密码"));
nvps.add(new BasicNameValuePair("DesNo", "手机号"));
nvps.add(new BasicNameValuePair("Msg", "短信内容【签名】"));
nvps.add(new BasicNameValuePair("Channel", "通道号"));
String post=httpPost(url,nvps); //post请求
String getparam="userCode=用户名&userPass=密码&DesNo=手机号&Msg=短信内容【签名】&Channel=通道号";
String result=httpGet(url,getparam); //get请求
}public static String httpPost(String url,Listparams) {
String result= "";try{
HttpClient httpclient= newDefaultHttpClient();
HttpPost httpPost= newHttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response=httpclient.execute(httpPost);
HttpEntity entity=response.getEntity();if (entity != null) {
InputStream instreams=entity.getContent();
result=convertStreamToString(instreams);
System.out.println(result);
}
}catch(Exception e) {
}returnresult;
}public staticString httpGet(String url,String params){
String result="";try{
HttpClient client=newDefaultHttpClient();if(params!=""){
url=url+"?"+params;
}
HttpGet httpget=newHttpGet(url);
HttpResponse response=client.execute(httpget);
HttpEntity entity=response.getEntity();if (entity != null) {
InputStream instreams=entity.getContent();
result=convertStreamToString(instreams);
System.out.println(result);
}
}catch(Exception e){}returnresult;
}public staticString convertStreamToString(InputStream is) {
BufferedReader reader= new BufferedReader(newInputStreamReader(is));
StringBuilder sb= newStringBuilder();
String line= null;try{while ((line = reader.readLine()) != null) {
sb.append(line+ "\n");
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{
is.close();
}catch(IOException e) {
e.printStackTrace();
}
}returnsb.toString();
}