HTTP request
Description: Refer to HttpClient, Httpcore, commons-logging three jar packages here
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.util.*;
Import Java.security.MessageDigest;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.client.methods.HttpGet;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicNameValuePair;
Import org.apache.http.*;
Import Javax.crypto.SecretKey;
Import Javax.crypto.spec.DESKeySpec;
Import Javax.crypto.spec.IvParameterSpec;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.Cipher;
public static void Main (string[] args) {
String url= "Http://xxx.com/api/MsgSend.asmx/SendMes";
list<namevaluepair> Nvps = new arraylist<namevaluepair> ();
Nvps.add (New Basicnamevaluepair ("Usercode", "User Name"));
Nvps.add (New Basicnamevaluepair ("Userpass", "password"));
Nvps.add (New Basicnamevaluepair ("Desno", "mobile phone Number"));
Nvps.add (New Basicnamevaluepair ("MSG", "SMS Content" signature ")");
Nvps.add (New Basicnamevaluepair ("channel", "aisle Number"));
String Post=httppost (URL,NVPS); POST request
String getparam= "usercode= user name &userpass= password &desno= mobile phone number &msg= SMS Content" signature "&channel= channel number";
String Result=httpget (Url,getparam); GET request
}
public static string HttpPost (string url,list<namevaluepair> params) {
String result = "";
try {
HttpClient HttpClient = new Defaulthttpclient ();
HttpPost HttpPost = new HttpPost (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) {
}
return result;
}
public static string HttpGet (string url,string params) {
String result= "";
try{
HttpClient client=new defaulthttpclient ();
if (params!= "") {
url=url+ "?" +params;
}
HttpGet httpget=new httpget (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) {}
return result;
}
public static String convertstreamtostring (InputStream is) {
BufferedReader reader = new BufferedReader (new InputStreamReader (IS));
StringBuilder sb = new StringBuilder ();
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 ();
}
}
return sb.tostring ();
}
WebService request
public static void Main (string[] args) {
Org.tempuri.MsgSend service = new Org.tempuri.MsgSend ();
Org.tempuri.MsgSendSoap port = Service.getmsgsendsoap ();
String result= port.sendmes ("username", "password", "Phone number", "SMS Content" signature "", "channel number");
SYSTEM.OUT.PRINTLN (result);
}
SMS Platform Interface Call method reference