HTTP错误提取URL。状态= 400

时间:2014-05-31 03:50:58

标签: jsoup

JSoup似乎与我尝试的所有网址一起使用,但是这个网站给了我400错误。

String url = "http://localad.walmart.com?storeref=3008&forceview=y";

Response response = Jsoup.connect(url.replaceAll(" ", "%20"))
    .method(Method.GET)
    .userAgent("Mozilla")
    .followRedirects(false)
    .timeout(5000)
    .data("pragma", "no-cache")
    .execute(); 

我得到的错误是:

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=400, URL=http://localad.walmart.com?storeref=3008&forceview=y&pragma=no-cache
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)

3 个答案:

答案 0 :(得分:1)

当JSoup在400 Bad Request上引发致命异常时,这会有些烦人,因为这会完全终止任何正在运行的进程,即使包裹在try / catch中也是如此。可是

对此有一个解决方案,在您的初始连接URL上附加方法;

.ignoreHttpErrors(true)

例如;

Jsoup.connect(url).ignoreHttpErrors(true).execute().statusCode();

然后为您提供正式的“ 400”状态代码,而不是引发致命异常。

我知道这是一篇旧文章,但是当我在寻找解决方案的时候遇到了这个话题,以供参考。

答案 1 :(得分:0)

400是错误请求。

您应该尝试URLEncoder.encode(url, "UTF-8")而不是replaceAll

答案 2 :(得分:0)

在不依赖JSoup的情况下移动以解析中间件(重定向的URL)。我需要最终的重定向URL(JSoup在使用时没有任何问题),所以使用以下代码来获取它。

import java.net.URI;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.protocol.HttpContext;

public class MyRedirectHandler extends DefaultRedirectStrategy {

    public URI lastRedirectedUri;

    @Override
    public boolean isRedirected(HttpRequest request, HttpResponse response, 
        HttpContext context) {
    try {
        return super.isRedirected(request, response, context);
     } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
    }

    @Override
    public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context)
        throws ProtocolException {

        lastRedirectedUri = super.getLocationURI(request, response, context);
        return lastRedirectedUri;
    }

调用代码:

    DefaultHttpClient httpclient = new DefaultHttpClient();
    String url2 = "http://localad.walmart.com/walmart/new_user_entry.aspx?storeref=3008&forceview=y";
    MyRedirectHandler handler = new MyRedirectHandler();
    httpclient.setRedirectStrategy(handler);

    HttpGet get = new HttpGet(url2);
    httpclient.execute(get);

    String lastUrl = url2;
    if (handler.lastRedirectedUri != null) {
        lastUrl = handler.lastRedirectedUri.toString();
    }