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調用最基本的包