|
本帖最后由 bingbing4647 于 2014-9-29 13:14 编辑
String result =doPost(props.getProperty("doPost").trim(), param, "utf-8", true);
public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) throws Exception {
StringBuffer response=new StringBuffer();
HttpClient client=new HttpClient();
PostMethod method=new PostMethod(url);
// 设置Http Post数据
if(params != null) {
NameValuePair[] data= new NameValuePair[params.size()];
int i=0;
for(Map.Entry<String, String> entry: params.entrySet()) {
data=new NameValuePair(entry.getKey(), entry.getValue());
i++;
}
method.setRequestBody(data);
}
try {
client.executeMethod(method);
if(method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader=new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while((line=reader.readLine()) != null) {
if(pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch(IOException e) {
e.printStackTrace();
} catch (Exception e){
}finally {
method.releaseConnection();
}
return response.toString();
}
-------------------------------------------------------------
-------------------------------------------------------------
public static String sendGet(String url) {
String result="";
try {
String urlName=url;
URL U=new URL(urlName);
URLConnection connection=U.openConnection();
connection.setRequestProperty("Connection", "close");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
connection.connect();
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line=in.readLine()) != null) {
result+=line;
}
in.close();
} catch(Exception e) {
System.out.println(e);
}
return result;
}
|
|