2016-05-06 4 views
0

私はthis siteにログインし、いくつかのデータを収集しようとしています。 私は下記の共有コードからユーザー名とパスワードを削除しましたが、最初のURLとパスワードを実行して実行すると、ログインが成功したことを示す応答が表示されますが、Cookieを保存して、それがちょうどハングするサイトのクエリ。エラーも応答もありません。私は何時間も遊んだけど、それを得ることはできない。そのは、このリンクを使用して実行client.execute(request)のthatsで立ち往生ます(http:// sef.imapp COM/ilinks /プロパティUPIN = US120860131120280120 =カンプ&距離= 0.5 &レポート)私はhttp://www.mkyong.com/java/apache-httpclient-examples/からコードをコピーし最初のログインが成功した後、HttpGet client.execute(request)からの応答がありません

(パートを3)、私は、ログインhtmlのページを表示する応答が得られるまで変更を加えたが、私はちょうどクエリのURLを実行することはできません。

詳細については、ご理解いただきありがとうございます。 ありがとうございます。

package connectors; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.CookieHandler; 
import java.net.CookieManager; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.message.BasicNameValuePair; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class HttpCilentExampleToMls { 

    private String cookies; 
    private HttpClient client = HttpClientBuilder.create().build(); 
    private final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.36 Safari/537.36"; 

    public static void main(String[] args) throws Exception { 

    //Login page Changed from google address 
    String url = "http://sef.imapp.com/ilinks/search"; 
    //Search page Changed from gmail address 
    String mlsUrl = "http://sef.imapp.com/ilinks/property?upin=US120860131120280120&report=comps&distance=0.5"; 

    // make sure cookies is turn on 
    CookieHandler.setDefault(new CookieManager()); 

    HttpCilentExampleToMls http = new HttpCilentExampleToMls(); 

    String page = http.GetPageContent(url); 

    List<NameValuePair> postParams = 
       // Changed from "Username" and "Password" 
       http.getFormParams(page, "Username","Password"); 

    http.sendPost(url, postParams); 
    String result = http.GetPageContent(mlsUrl); 
    } 

    private void sendPost(String url, List<NameValuePair> postParams) 
     throws Exception { 

    HttpPost post = new HttpPost(url); 

    // add header 
    post.setHeader("Host", "sef.imapp.com"); 
    post.setHeader("User-Agent", USER_AGENT); 
    post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
    post.setHeader("Accept-Language", "en-US,en;q=0.8"); 
    post.setHeader("Cookie", getCookies()); 
    post.setHeader("Connection", "Keep-Alive"); 
    post.setHeader("Referer", "http://sef.imapp.com/ilinks/login?logout=true"); 
    post.setHeader("Content-Type", "application/x-www-form-urlencoded"); 

    post.setEntity(new UrlEncodedFormEntity(postParams)); 

    HttpResponse response = client.execute(post); 

    int responseCode = response.getStatusLine().getStatusCode(); 

    System.out.println("\nSending 'POST' request to URL : " + url); 
    System.out.println("Post parameters : " + postParams); 
    System.out.println("Response Code : " + responseCode); 

    BufferedReader rd = new BufferedReader(
       new InputStreamReader(response.getEntity().getContent())); 

    StringBuffer result = new StringBuffer(); 
    String line = ""; 
    while ((line = rd.readLine()) != null) { 
     result.append(line); 
     System.err.println(line); 
    } 

    // System.out.println(result.toString()); 

    } 

    private String GetPageContent(String url) throws Exception { 

    HttpGet request = new HttpGet(url); 

    request.setHeader("User-Agent", USER_AGENT); 
    request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
    request.setHeader("Accept-Language", "en-US,en;q=0.8"); 
    HttpResponse response = client.execute(request); 
    int responseCode = response.getStatusLine().getStatusCode(); 

    System.out.println("\nSending 'GET' request to URL : " + url); 
    System.out.println("Response Code : " + responseCode); 
    BufferedReader rd = new BufferedReader(
       new InputStreamReader(response.getEntity().getContent())); 

    StringBuffer result = new StringBuffer(); 
    String line = ""; 
    while ((line = rd.readLine()) != null) { 
     result.append(line); 
    } 
    // set cookies 
    setCookies(response.getFirstHeader("Set-Cookie") == null ? "" : 
        response.getFirstHeader("Set-Cookie").toString()); 
    return result.toString(); 
    } 

    public List<NameValuePair> getFormParams(
      String html, String username, String password) 
      throws UnsupportedEncodingException { 

    System.out.println("Extracting form's data..."); 

    Document doc = Jsoup.parse(html); 

    // Google form id 
    Element loginform = doc.getElementById("standardLogin"); 
    Elements inputElements = loginform.getElementsByTag("input"); 


    List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 

    for (Element inputElement : inputElements) { 
     String key = inputElement.attr("name"); 
     String value = inputElement.attr("value"); 

     if (key.equals("user")) 
      value = username; 
     else if (key.equals("passwd")) 
      value = password; 

     paramList.add(new BasicNameValuePair(key, value)); 

    } 

    return paramList; 
    } 

    public String getCookies() { 
    return cookies; 
    } 

    public void setCookies(String cookies) { 
    this.cookies = cookies; 
    } 

} 
+0

あなたは 'https'サイトを使用しないでください。私は 'https:// sef.imapp.com/ilinks/search'を試し、ユーザー名とパスワードも尋ねます。 –

+0

これは私がそれをやってみると得られるエラーです。スレッド "main"の例外javax.net.ssl.SSLProtocolException:ハンドシェイクアラート:unrecognized_name \t at sun.security.ssl.ClientHandshaker.handshakeAlert(ClientHandshaker.java:1429) – aronowt

+0

誰かが私を助けることができますか?私はそれを認証なしで別のサイトのために働かせることができました。私はちょうどそのクエリで1つのURLを使用し、応答を得て、getElementsByを使用して私のデータをプルすることができました... – aronowt

答えて

0

私はウェブサイトから正確mkyongのコードとコメントをしようとして同じ問題を抱えていたが、それを解決:HttpGetまたはHttpPostオブジェクトを使用した後、その方法releaseConnection()を使用します。 8 •ヶ月前

アレックスLoginovこんにちは、誰かのために役に立つかもしれません: 後、あなたが応答を受信し、(HTTPGET)HttpPostを閉じてください: post.releaseConnection();それ以外の場合は、2回目以降は無料の 接続を持たず、無期限にリリースする必要があります。 例外がスローされます。

関連する問題