与java.net.HttpURLConnection相比,Apache HttpClient是否以不同方式处理GET请求?
我尝试向URL发出GET请求,该URL使用这两种方法返回重定向。虽然来自HttpURLConnection的响应代码按预期返回302,但使用HttpClient进行相同的调用会产生200.
以下是我的代码:
Dim con As New SqlClientConnection( . . .)
Try
' DB Operations (create, read, update, delete) here
con.open()
Catch SqlClientException (ex)
' Deal with exception
Finally
' Code here will always run, whether or not the try code succeeded
' or if you wound up in a catch block. You can safely call .close()
' or (as shown below) .dispose() because if the connection is already
' closed/disposed, the call doesn't do anything.
' Dispose is what you want - it will not only close the
' connection, but it will release the .NET reference to
' the remote resource, freeing up that resource (the database
' in this case) to serve other clients.
con.Dispose()
End Try
这是我第一次使用Apache HttpClient,所以我的代码可能出错了。
感谢。
答案 0 :(得分:1)
Handling GET requests - Apache HttpClient vs java.net.HttpURLConnection 如果您需要遍历一系列重定向,则应为HttpPost / HttpGet(HttpRequestBase)设置禁用重定向,例如:
public void disableRedirect(HttpRequestBase request) {
request.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
}
之后,您将获得带有response.getStatusLine().getStatusCode()
的302响应代码,并且可以读取标题为@Ascalonian说
答案 1 :(得分:0)
尝试关闭自动重定向处理。很可能HttpClient重定向到302响应中指定的位置,而HUC由于某种原因不会