views:

484

answers:

1

Hi all, I need my program to send a request to a server. The problem is, the server only recognizes ös,äs und üs, but JAVA and/or Android don't know them. `How can I send a request with a String like "Hermann-Löns" without JAVA/Android "changing" the ö.... Oh and btw., "oe" isn't recognized by the server too, already tried that...

thx for help!

@BalausC:

I changed your code to:

I'm not sure if this is how you refer to the right fields...

String url = "http://busspur02.aseag.de/bs.exe?SID=473A2&ScreenX=1440&ScreenY=900&CMD=CR&DatumT=30&DatumM=4&DatumJ=2010&AbfAnk=Abf&ZeitH=10&ZeitM=45&Intervall=60&Loeschen=%28N%29eue+Suche";
        String charset = "CP1252";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("HTO", start_from));
        params.add(new BasicNameValuePair("HT1", destination));
        UrlEncodedFormEntity query = new UrlEncodedFormEntity(params, charset);

            HttpPost post = new HttpPost(url);
            post.setEntity(query);
            InputStream response = new DefaultHttpClient().execute(post).getContent();
            // Now do your thing with the facebook response.

I can't compile because I get an error saying:

The method getContent() is undefined for the type HttpResponse

If I delete getContent() is says:

Type mismatch: cannot convert from HttpResponse to InputStream

One more thing: I use htmlparser (http://htmlparser.sourceforge.net/) to parse the resulting website. How do I access the resulting html site to parse it? Because otherwise I would have to rewrite nearly all of my code to get the results..

+4  A: 

To the point, you need to use java.net.URLEncoder with the appropriate character encoding to encode the special characters in request parameters.

String param = URLEncoder.encode("Hermann-Löns", "CP1252");

Ensure that you specify an Accept-Charset: CP1252 header in the HTTP request. For a more complete code example how to fire a HTTP request with correct encoding, check this answer which I posted a hour ago. It also covers a HttpClient example which is also included in Android.

BalusC
thx for your answer. I tried it and it works, well, it WOULD work, but here is the problem: I just paste the user input into the url the server would receive if I made a regular request in a webbrowser . afterwards I parse the page which would be displayed. I saw that the "server" uses this encoding in UTF-8, but only AFTER he got the input. that means he gets my UTF-8 encoded Strings as input not as a "direct" call..For example: "Test Haus" becomes "Test%2BHouse" BEFORE the server even gets the input to use it. then he does the same encoding and tries to use it...
Jayomat
The server needs to `URLDecode` it. Decent Java webservers/servletcontainers should do this automagically (at least, if you specified the correct `Content-Type` in the request header, I'm however not sure if HttpClient does that). Which server are you talking about? Is this under your full control?
BalusC
Jayomat
That site uses Windows 1252 (ANSI) encoding... I'd replace `"UTF-8"` by `"CP1252"` in the code.
BalusC
"Ensure that you specify an Accept-Charset: UTF-8 header in the HTTP request." what do you mean by that?
Jayomat
See the link behind "check this answer".
BalusC
I created a new answer, the comments aren't the right place to post code etc...
Jayomat
You should have edited your question to include the update :) I was writing the code from top of head and it turns out that I was forgotten a `getEntity()` in between :) For yours sake, you could also just check the API docs, tutorials and examples at the HttpClient site ;)
BalusC
ok sorry, I edited my first post. I thought it would be easier for other people who read this to follow my changes....... Now I just need to figure out what to do with the response variable.. :) I just want to get the htmlpage I would have gotton if I entered the params in a browser...
Jayomat
It's in the `InputStream`. Most HTML parsers can just accept an `InputStream` as some method argument, e.g. `parse(stream)`. Consult their documentation. If you stucks, better ask a new question, that's an entirely different subject.
BalusC
Ok thank you so far. I see what I can do and greatly appreciate you effort!
Jayomat
OK I got the stream into a string. However, it just took the page as-is, not "putting" anything into the page and sending the actual request :/
Jayomat
Probably more request parameters are missing. The submit button itself maybe? Install a HTTP header debugger (Firebug? Fiddler?), submit the real form and track which parameters exactly are actually been sent and then substitute the same in your Java code. Also in some cases the `User-Agent` request header might be deteminative; try to set a "normal" webbrowser user agent string then, e.g. the one of Firefox. After all, your initial question is answered for long. This is a different problem. If you still stucks, ask a new question.
BalusC