HttpPost - ClientProtocolException

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

HttpPost - ClientProtocolException

Postby xoddam » Fri Nov 04, 2011 3:51 pm

Hello there!

First of all please understand my situation: I am at the end with myself. Im struggeling with this problem for more than 20 hours in total and yet no success! :(

Please help me. Ask for whatever you want I just need someones help... :?
(I swear to god, if I get help here :) I will write the best tutorial EVER and post it allover the internet so people with similar problems can find this.)


In short:
WHAT: Basicly what I want to do is "POST" data to a website and parse the result
HOW: Using HttpClient HttpPost and HttpResponse
WHAT CONFUSES ME: Many many things of APACHE.HTTP work different on PC (Java) and on Android
PROBLEM: In these last three days im getting: - error code 417 (requirements), ClientProtocolExceptions

In detail:
1. There is this website: http://na.leagueoflegends.com/ladders/solo-5x5
2. And you can search for a player by his name : example "Jaiybe"
3. You will be redirected (CODE: 302) to the page: http://na.leagueoflegends.com/ladders/s ... =29&page=1 (This link is in the Location header of the response (but I dont get one in Android) )

Okay and now programmatically:

JAVA: All works fine on JAVA (PC), try running this - THIS DOES EXACTLY WHAT I NEED, but on PC not Android :( :

Code: Select all
// Preparing the CLIENT and POST Method
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://na.leagueoflegends.com/ladders/solo-5x5");
      
      try {
         // Add your POST METHOD attributes
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
         nameValuePairs.add(new BasicNameValuePair("op", "Search"));
         nameValuePairs.add(new BasicNameValuePair("player", "Jaiybe"));
         nameValuePairs.add(new BasicNameValuePair("ladder_id", "3"));
         nameValuePairs.add(new BasicNameValuePair("form_build_id",
               "form-526370b788622996caa3669e7b975ccf"));
         nameValuePairs.add(new BasicNameValuePair("form_id",
               "ladders_filter_form"));
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         // Execute HTTP Post Request
         HttpResponse response = httpclient.execute(httppost);
         
         // RESPONE THAT WORKS WITH JAVA
         System.out.println("Location:");
         String LocationHeader = response.getFirstHeader("location").getValue();
         System.out.println(LocationHeader);
         System.out.println();
         
         // To get the BODY I would have to parse that again - since its not REDIRECTING automatically
         HttpClient httpclient2 = new DefaultHttpClient();
         HttpPost httppost2 = new HttpPost(LocationHeader);
         response = httpclient2.execute(httppost2);
         System.out.println("And EVEN the response body:");
         System.out.println(EntityUtils.toString(response.getEntity()));



#1 approach : ANDROID: And now the android code (since android has nothing like System.Out.Println I use TextView.SetText(thisMethod()) and the results are sent back as strings):

Android MAIN JAVA CLASS:
Code: Select all
package sk.inmotionmedia.tester;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class AndroidTesterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        String Text = getHttpData.getRanking();
       
        TextView a = (TextView) findViewById(R.id.textView1);
        a.setMovementMethod(new ScrollingMovementMethod());
       
        a.setText(Text);
       
    }
}


And the getHttpData class with getHttpData.getRanking() Method.

Code: Select all
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class getHttpData {

   // REGIONS:
   // euw - EU WEST
   // eune - EU NORDIC & EAST
   // na - NORTH AMERICA
   // QUEUE:
   // solo-5x5
   // premade-5x5
   // premade-3x3
   // PLAYER NAME: Desired player name
   public static String getRanking() {
      try {

         // Creating new HttpClient
         HttpClient httpclient = new DefaultHttpClient();

         // Preparing the POST Method
         HttpPost httppost = new HttpPost(
               "http://na.leagueoflegends.com/ladders/solo-5x5");

         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
         nameValuePairs.add(new BasicNameValuePair("op", "Search"));
         nameValuePairs.add(new BasicNameValuePair("player", "salce"));
         nameValuePairs.add(new BasicNameValuePair("ladder_id", "3"));
         nameValuePairs.add(new BasicNameValuePair("form_build_id",
               "form-586cb8cc17f1f66b91de512be7257bb5"));
         nameValuePairs.add(new BasicNameValuePair("form_id",
               "ladders_filter_form"));
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         HttpResponse response = httpclient.execute(httppost);

         // Now lets see the results of our ACTION
         Header[] a = response.getAllHeaders();

         // The results are collected in this String
         String value = "";

         // Lets get ALL Headers of the Response
         for (int i = 0; i < a.length; i++) {
            value = value + " " + i + " " + a[i].getName() + " : "
                  + a[i].getValue() + " \n ";
         }

         // And in the end the STATUS LINE of the request
         value = value + " " + response.getStatusLine().toString();

         // return the String to the SetText Method and lets see the results.
         return value;

         // Possible EXCEPTIONS
      } catch (ClientProtocolException e) {
         return "1error";
      } catch (IOException e) {
         return "2error";

      }

   }
}


And when running the result:
Image


#2 approach : ANDROID:
Hmmm Im thinking, ok we are getting somewhere, expectations failed, strange - its the same code as on JAVA (PC) - so Im thinking, hmmm, what could that be, maybe there is less information send with the post method so lets check that with Google Chrome.

So POST MESSAGE RESULTS, when looking for a player (In google chrome wit hINSPECT ELEMENT - Network):

Image

Ok so my browser send that information with the POST method, maybe I shall include them?

Lets TRY:
Code with the additional headers included:
Code: Select all
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class getHttpData {

   // REGIONS:
   // euw - EU WEST
   // eune - EU NORDIC & EAST
   // na - NORTH AMERICA
   // QUEUE:
   // solo-5x5
   // premade-5x5
   // premade-3x3
   // PLAYER NAME: Desired player name
   public static String getRanking() {
      try {

         // Creating new HttpClient
         HttpClient httpclient = new DefaultHttpClient();

         // Preparing the POST Method
         HttpPost httppost = new HttpPost(
               "http://na.leagueoflegends.com/ladders/solo-5x5");

         // POST FORM Values
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
         nameValuePairs.add(new BasicNameValuePair("op", "Search"));
         nameValuePairs.add(new BasicNameValuePair("player", "salce"));
         nameValuePairs.add(new BasicNameValuePair("ladder_id", "3"));
         nameValuePairs.add(new BasicNameValuePair("form_build_id",
               "form-586cb8cc17f1f66b91de512be7257bb5"));
         nameValuePairs.add(new BasicNameValuePair("form_id",
               "ladders_filter_form"));
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         // ADDITIONAL HEADERS
         httppost.setHeader("Accept",
               "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
         httppost.setHeader("Accept-Charset",
               "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
         httppost.setHeader("Accept-Encoding", "gzip,deflate,sdch");
         httppost.setHeader("Accept-Language", "en-US");
         httppost.setHeader("Cache-Control", "max-age=0");
         httppost.setHeader("Connection", "keep-alive");
         httppost.setHeader("Content-Length", "114");
         httppost.setHeader("Content-Type",
               "application/x-www-form-urlencoded"); //
         httppost.setHeader(
               "Cookie",
               "testcookie=expanded; bblastvisit=1318319486; bblastactivity=0; __utma=123516831.1570927398.1318045437.1318045437.1318045437.1; __utmb=123516831.2.9.1320413027481; __utmc=123516831; __utmz=123516831.1318045437.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=league%20of%20legends; testcookie=expanded; __utma=47448120.2010862.1318045443.1320396552.1320413031.22; __utmb=47448120.2.10.1320413031; __utmc=47448120; __utmz=47448120.1320413031.22.15.utmcsr=eune.leagueoflegends.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _chartbeat2=p3ljo9r27np5wik4.1318045451638");
         httppost.setHeader("Host", "na.leagueoflegends.com");
         httppost.setHeader("Origin", "http://na.leagueoflegends.com");
         httppost.setHeader("Referer",
               "http://na.leagueoflegends.com/ladders/solo-5x5"); //
         httppost.setHeader(
               "User-Agent",
               "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.10 Chromium/14.0.835.202 Chrome/14.0.835.202 Safari/535.1");

         HttpResponse response = httpclient.execute(httppost);

         // Now lets see the results of our ACTION
         Header[] a = response.getAllHeaders();

         // The results are collected in this String
         String value = "";

         // Lets get ALL Headers of the Response
         for (int i = 0; i < a.length; i++) {
            value = value + " " + i + " " + a[i].getName() + " : "
                  + a[i].getValue() + " \n ";
         }

         // And in the end the STATUS LINE of the request
         value = value + " " + response.getStatusLine().toString();

         // return the String to the SetText Method and lets see the results.
         return value;

         // Possible EXCEPTIONS
      } catch (ClientProtocolException e) {
         return "1error";
      } catch (IOException e) {
         return "2error";

      }

   }
}




RESULT: ClientProtocolException !!! :( :(


And now Im stuck, when I take some details out, I get 407 - Expectations Failed, and when I add all of them I get Exception...


Guys ... You know my problem, you know my issue, you know what I need (desperately) please help me, or help me finding another way how to get to the redirected URL, so i get the redirected content...


Thank you

VERY VERY VERY MUCH !

Oliver
xoddam
Freshman
Freshman
 
Posts: 6
Joined: Tue May 18, 2010 4:31 pm

Top

Re: HttpPost - ClientProtocolException

Postby jonbonazza » Mon Nov 07, 2011 5:15 pm

The first approach you took seemed correct. The only thing I noticed was that the url you provided didn't link to a PHP file. What are you trying to POST to?
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: HttpPost - ClientProtocolException

Postby xoddam » Mon Nov 07, 2011 11:58 pm

Hmmm I never considered that as a problem, becasue the 1st JAVA code actually works. Hmm I mean like I have no idea if the 1st site is a PHP, maybe it is (with clean URL) maybe it is not. Well I taught like, I dont have to care, since it has a form that I will send as FILLED OUT with my post request - their php server has to handle that.
xoddam
Freshman
Freshman
 
Posts: 6
Joined: Tue May 18, 2010 4:31 pm

Re: HttpPost - ClientProtocolException

Postby xoddam » Tue Nov 08, 2011 12:31 am

FOUND THE PROBLEM !

httpclient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);

version 1_0 works and 1_1 does not for me !!!!

Where are my 3 days of headache? :)))
xoddam
Freshman
Freshman
 
Posts: 6
Joined: Tue May 18, 2010 4:31 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 7 guests