/**
* 请求
* @author shmily
*
*/
public class HttpUtil {
//根据url发送请求,并获取返回的信息
public static String post(String url) {
String sTotalString = null;
try {
URL l_url = new URL(url);
HttpURLConnection l_connection = (HttpURLConnection) l_url.openConnection();
l_connection.connect();
InputStream l_urlStream;
l_urlStream = l_connection.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
String sCurrentLine = null;
while ((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString += sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
}
return sTotalString;
}
}
* 请求
* @author shmily
*
*/
public class HttpUtil {
//根据url发送请求,并获取返回的信息
public static String post(String url) {
String sTotalString = null;
try {
URL l_url = new URL(url);
HttpURLConnection l_connection = (HttpURLConnection) l_url.openConnection();
l_connection.connect();
InputStream l_urlStream;
l_urlStream = l_connection.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
String sCurrentLine = null;
while ((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString += sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
}
return sTotalString;
}
}
本文介绍了一个简单的HTTP请求工具类的实现方式,该工具类能够通过POST方法发送HTTP请求并接收响应信息。代码示例使用Java语言编写,展示了如何创建URL对象、打开连接、读取输入流并整合为字符串。
3万+

被折叠的 条评论
为什么被折叠?



