第一步导包
导入的jar包有 1:httpclient 2:httpcore 3commons-logging-api 4:json包 (转换json的jar包是很多的每个系统的方法都是不一样) json-io-4.3.0
第二步 写代码
GET请求
public static void doGet(String url) throws Exception{
//url 路径
HttpClient client = new DefultHttpClient();
HttpGet getmeth = new HttpGet(url);
try {
HttpResponse statueCode = cleint.execute(getmeth);
System.out.println(response1.getStatusLine());//状态码 200成功 500服务器内部报错这个时候要看服务端的报错日志 404 路径错误
}catch(IOException e){
e.printStackTrace();
}
}
POST请求
public static void doPost(String url,String param) throws Exception{
HttpClient client = new DefultHttpClient();
HttpPost postmeth = new HttpPost(url);
postmeth.setEntity(new StringEntity(param)); //post请求参数 param可以在请求之前进行json转换
try{
HttpResponse statueCode = cleint.execute(getmeth);
System.out.println(response1.getStatusLine());//状态码 200成功 500服务器内部报错这个时候要看服务端的报错日志 404 路径错误
}catch(IOException e){
e.printStackTrace();
}
}
转json方法
public static String json( String param){
List<String> list = new ArrayList<String>();
list.add(param);
String json = JsonWriter.objectToJson(list)'
return json;
}
main方法调用请求
public static void main(String[] args) {
json(param);
try {
doGet(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
doPost(url ,param);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
webservice 基础知识