请教http摘要认证客户端的Java实现
发布网友
发布时间:2022-05-02 22:59
我来回答
共1个回答
热心网友
时间:2022-06-28 02:36
HttpURLConnection hc = null;
try
{
hc = (HttpURLConnection) new URL(url).openConnection();
hc.setConnectTimeout(10000);
hc.setReadTimeout(10000);
hc.setDoInput(true);
hc.setDoOutput(true);
hc.setUseCaches(false);
String content = param;
DataOutputStream out = new DataOutputStream(hc.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
//从hc中取出sessionId
for (int i = 1; (key = hc.getHeaderFieldKey(i)) != null; i++)
{
if (key.equalsIgnoreCase("Set-Cookie"))
{
sessionId = hc.getHeaderField(key);
sessionId = sessionId.substring(0, sessionId.indexOf(";"));
break;
}
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(hc.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null)
{
break;
}
reader.close();
catch (Exception e)
{
//
//e.printStackTrace();
return false;
}
finally
{
// 断开连接
hc.disconnect();
}
希望能帮到你!