1.这是通过json传值进行调用
(1)public static void appadd() {
try {
//创建连接
URL url = new URL(ADD_URL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
//POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
JSONObject obj = new JSONObject();
obj.element("mesConent", java.net.URLEncoder.encode("郭郭"));
obj.element("phoneList", "15662165280");
out.writeBytes(obj.toString());
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
(2)xmlInfo是json字符窜
public static String doHttpPost(String xmlInfo,String URL){
System.out.println("发起的数据:"+xmlInfo);
InputStream instr = null;
java.io.ByteArrayOutputStream out = null;
try{
URL url = new URL(URL);
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setUseCaches(false);
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
printout.writeBytes(xmlInfo);
printout.flush();
printout.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlCon.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
return sb.toString();
}catch(Exception e){
e.printStackTrace();
return "0";
}
finally {
try {
out.close();
instr.close();
}catch (Exception ex) {
return "0";
}
}
}
2 通过参数传值例如url传值 ?a=?&b=?
利用httpcliend
String url="";
HttpClient client1=new HttpClient();
client1.setConnectionTimeout(30000);
PostMethod postMethod = new PostMethod(url);
client1.getParams().setContentCharset("UTF-8");
postMethod.setParameter("businesId", "1");
postMethod.setParameter("itemId", "2");
try {
client1.executeMethod(postMethod);
JSONObject result_code= (JSONObject) JSONObject.parse(postMethod.getResponseBodyAsString());
String result_id=result_code.getString("state");
String error_msg=result_code.getString("error");
if(result_id.equals("200")){
}
} catch (HttpException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
注意的事情
就是在提交请求的时候会出现编码规范
application/x-www-form-urlencoded 在发送前编码所有字符(默认)
multipart/form-data 不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
text/plain 空格转换为 "+" 加号,但不对特殊字符编码。
这些是httpclient调用最基本的包