I am developing an app plays song and communicates with a server. The methods are addMusic, deleteMusic, and requestMusicList. All of them uses http GET method.
Here's The Case:
deleteMusic, and requestMusicList works fine in all phones. Except for addMusic, which only works in some phone.
addMusic only works in Samsung phone. While in nuu, mi, and meizu. Those are the phones that we got here, so we only tested the app with those phones.
My Problem:
Since the parameters included in the addMusic url string contains Chinese characters, I think this is the cause of the problem. Here is the log.
08-06 15:00:22.102: V/HttpConnectionManager(13211): Get url string is http://115.28.6.88:7100/cgi-bin/v1/music_add?content_url=http://fdfs.xmcdn.com/group12/M07/03/65/wKgDW1VQa-6iJ2e1AB7JjcEKs2E684.mp3&cover_image_url=http://fdfs.xmcdn.com/group11/M02/03/7B/wKgDa1VQbA2j2mOpAAiTLl27BzM875_mobile_small.jpg&dev_id=test101&duration=252×tamp=1438844422&title=《Couldyoustaywithme》张靓颖(电视剧《洋嫁》主题曲)&type=type_habit&sign=f834a4667004cebdac1516584b4a4930
08-06 15:00:22.272: I/Adreno200-EGLSUB(13211): <ConfigWindowMatch:2252>: Format RGBA_8888.
08-06 15:00:22.342: V/HttpConnectionManager(13211): Response code from GET: 200
08-06 15:00:22.352: V/DetailManager(13211): Handled http response is {"retcode": 100004, "retinfo": "check sign fail, request_sign:[F834A4667004CEBDAC1516584B4A4930], mysign:[EBFA4D2D40F20DF1B124375A0D9FAD38], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group12/M07/03/65/wKgDW1VQa-6iJ2e1AB7JjcEKs2E684.mp3&cover_image_url=http://fdfs.xmcdn.com/group11/M02/03/7B/wKgDa1VQbA2j2mOpAAiTLl27BzM875_mobile_small.jpg&dev_id=test101&duration=252×tamp=1438844422&title=?Couldyoustaywithme????????????????&type=type_habit]"}
08-06 15:00:22.352: V/AudioFragment(13211): Server request failed: check sign fail, request_sign:[F834A4667004CEBDAC1516584B4A4930], mysign:[EBFA4D2D40F20DF1B124375A0D9FAD38], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group12/M07/03/65/wKgDW1VQa-6iJ2e1AB7JjcEKs2E684.mp3&cover_image_url=http://fdfs.xmcdn.com/group11/M02/03/7B/wKgDa1VQbA2j2mOpAAiTLl27BzM875_mobile_small.jpg&dev_id=test101&duration=252×tamp=1438844422&title=?Couldyoustaywithme????????????????&type=type_habit]
As can be seen in the first line, this contains the URL that I sent. The URL contains these "张靓颖(电视剧《洋嫁》主题曲" Chinese characters under the title parameter. When the server returns a response, these characters became "??????????????".
Please take not that this issue only occurs in the said Android phones(mi, meizu, and nuu). For Samsung, this NEVER occured.
Here is the code of my addMusic method:
public void addMusic(final Music music)
{
new Thread(new Runnable()
{
#Override
public void run()
{
String contentURL = KEY_CONTENT_URL + "=" + music.mContentURL;
String coverImageURLData = "&" + KEY_COVER_IMAGE_URL + "=" + music.mCoverImageURL;
String deviceIDData = "&" + KEY_DEVICE_ID + "=" + mDeviceID;
String duration = "&" + KEY_DURATION + "=" + Long.toString(music.mDuration);
String timestamp = "&" + KEY_TIMESTAMP + "=" + Long.toString(System.currentTimeMillis()/1000);
String titleData = "&" + KEY_TITLE + "=" + music.mTitle.replaceAll("\\s", "");
String typeData = "&" + KEY_TYPE + "=" + music.mType;
String dataArrayString = contentURL + coverImageURLData + deviceIDData + duration + timestamp + titleData + typeData;
String key = "&" + KEY_KEY + "=" + KEY;
String sign = "&" + KEY_SIGN + "=" + Utilities.getMD5String(dataArrayString + key);
String parametersURL = dataArrayString + sign;
try
{
handleHttpResponse(mConnectionManager.get(URL_MAIN + URL_ADD_MUSIC + parametersURL), REQUEST_CODE_ADD_MUSIC);
}
catch (IOException e)
{
e.printStackTrace();
mListener.onServerRequestFail(e.getLocalizedMessage());
}
}
}).start();
}
Here is the code of Http GET:
public String get(String urlString) throws IOException
{
Log.v(LOG_TAG, "Get url string is " + urlString);
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = connection.getResponseCode();
Log.v(LOG_TAG, "Response code from GET: " + responseCode);
if(responseCode == RESPONSE_OK)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = reader.readLine()) != null)
{
response.append(inputLine);
}
reader.close();
return response.toString();
}
else
{
return ERROR_NO_RESPONSE;
}
}
I tried checking the defaultCharset, which is "UTF-8" for all phones tested.
How can I solve this? Please help. Many Thanks
------------ Tried URLEncoder.encode(music.mTitle.replaceAll("\s", ""), "UTF-8") ----------------
Here's the Log:
08-06 15:53:08.852: V/HttpConnectionManager(15061): Get url string is http://115.28.6.88:7100/cgi-bin/v1/music_add?content_url=http://fdfs.xmcdn.com/group6/M09/1E/0B/wKgDg1UrI8iw-rGzAAyaxOKiDrc706.mp3&cover_image_url=http://fdfs.xmcdn.com/group6/M00/1E/18/wKgDg1UrJp7TMEMMAAP5a37TPkE290_mobile_small.jpg&dev_id=test101&duration=103×tamp=1438847588&title=%E3%80%8Ayouaremysunshine%E3%80%8B%EF%BC%8D%E5%BC%A0%E9%9D%93%E9%A2%96%EF%BC%88%E7%94%B5%E5%BD%B1%E3%80%8A%E4%BD%95%E4%BB%A5%E7%AC%99%E7%AE%AB%E9%BB%98%E3%80%8B%E8%8B%B1%E6%96%87%E6%8F%92%E6%9B%B2&type=type_habit&sign=f7b8c5a0cb1fceb32a54b428311744ad
08-06 15:53:08.902: I/Adreno200-EGLSUB(15061): <ConfigWindowMatch:2252>: Format RGBA_8888.
08-06 15:53:19.692: V/HttpConnectionManager(15061): Response code from GET: 200
08-06 15:53:19.692: V/DetailManager(15061): Handled http response is {"retcode": 100004, "retinfo": "check sign fail, request_sign:[F7B8C5A0CB1FCEB32A54B428311744AD], mysign:[537721D351DF6C8B40C62C4F8672EADF], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group6/M09/1E/0B/wKgDg1UrI8iw-rGzAAyaxOKiDrc706.mp3&cover_image_url=http://fdfs.xmcdn.com/group6/M00/1E/18/wKgDg1UrJp7TMEMMAAP5a37TPkE290_mobile_small.jpg&dev_id=test101&duration=103×tamp=1438847588&title=\u300ayouaremysunshine\u300b\uff0d\u5f20\u9753\u9896\uff08\u7535\u5f71\u300a\u4f55\u4ee5\u7b19\u7bab\u9ed8\u300b\u82f1\u6587\u63d2\u66f2&type=type_habit]"}
08-06 15:53:19.702: V/AudioFragment(15061): Server request failed: check sign fail, request_sign:[F7B8C5A0CB1FCEB32A54B428311744AD], mysign:[537721D351DF6C8B40C62C4F8672EADF], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group6/M09/1E/0B/wKgDg1UrI8iw-rGzAAyaxOKiDrc706.mp3&cover_image_url=http://fdfs.xmcdn.com/group6/M00/1E/18/wKgDg1UrJp7TMEMMAAP5a37TPkE290_mobile_small.jpg&dev_id=test101&duration=103×tamp=1438847588&title=《youaremysunshine》-张靓颖(电影《何以笙箫默》英文插曲&type=type_habit]
I solved it. Thanks to #Andy Turner, and #lorenzo-s for the tips in the comment.
My mistake is that I encoded the string to UTF-8 before getting the MD5 checksum. I noticed that when I send the request(URLEncoded) to the server, the response containing the title is correct. The error was only about the MD5 sign check.
The correct way is to get the MD5 checksum first without encoding the title parameter. Then before appending the title parameter to the URL String, convert it to UTF-8.
Thank you #Andy Turner for reminding me that I have to deal with the "&" in the parameter.
Related
I am trying to connect my android app to shutterstock api so that it can search for some images there. It uses https scheme + Basic Authentication header to allow users for all search requests. I implemented the functionality in a regular java project using HttpsURLConnection and was able to get correct JSON responses.
The java code looks like this:
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();//proxy);
String username = "62c01aa824222683004b", password = "dc4ad748a75e4e69ec853ad2435a62b700e66164";
String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes("UTF-8"));
System.out.println(encoded.equals("Nj0jMDFhZWE4ZmE4MjY4MzAwNGI6ZGM0YWQ3NDhhNzVlNGU2gWVjODUzYWQ0ZmEzYTYyYjc7MGU2NjE2NA==")); // prints true
urlConnection.setRequestProperty("Authorization", "Basic "+encoded);
When ported this into Android, it was throwing an IOException with 401 error code. As explained in many posts on SO (like the one here), I modified the code accordingly with an extra try-catch as below:
String username = "62c01aa824222683004b", password = "dc4ad748a75e4e69ec853ad2435a62b700e66164", encoded = "";
encoded = Base64.encodeToString((username+":"+password).getBytes("UTF-8"), Base64.URL_SAFE);
Log.e("test", "encoded strings match:" + encoded.equals("Nj0jMDFhZWE4ZmE4MjY4MzAwNGI6ZGM0YWQ3NDhhNzVlNGU2gWVjODUzYWQ0ZmEzYTYyYjc7MGU2NjE2NA==") + "\n" + encoded); // prints false but string is same!!
URL url = new URL(reqUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic "+encoded);
try {
if (connection != null) {
connection.connect();
if (200 == connection.getResponseCode()) { // ---> throws IOException
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
Log.e("test", output);
response.append(output);
}
connection.disconnect();
return response.toString();
}
}
} catch (IOException e) {
try {
Log.e("test", e.getMessage()); // ---> prints "No authentication challenges found"
Log.e("test", connection.getResponseCode() + ":" + connection.getResponseMessage() + connection.getHeaderFields());
//---> prints 401:Unauthorized{null=[HTTP/1.1 401 Unauthorized], cache-control=[no-cache], Connection=[keep-alive], Content-Length=[38], Content-Type=[application/json; charset=utf8], Date=[Tue, 31 May 2016 14:11:28 GMT], Server=[nginx], X-Android-Received-Millis=[1464703888222], X-Android-Sent-Millis=[1464703887592], x-end-user-request-id=[f754ec7f-c344-431b-b641-360aabb70184], x-shutterstock-app-version=[apitwo-625], x-shutterstock-resource=[/v2/images/search]}
if (401 == connection.getResponseCode()) {
InputStream es = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(es));
String output;
while ((output = br.readLine()) != null) {
Log.e("test", output); // ---> prints {"message":"Invalid auth credentials"}
response.append(output);
}
connection.disconnect();
return response.toString();
} else {
Log.e("test","Could not connect! " + connection.getResponseCode() + ":" + connection.getResponseMessage() + ". " + connection.getRequestMethod());
}
}catch (Exception e1){e1.printStackTrace();}
}
I was unable to check the response headers in Firefox's Rest client because it does not send the request to server when I add the Authentication header.
So the questions here are:
Is this the right way to handle the 401 error in Android? Will I get the JSON response in the inner try-catch?
The java program uses exactly the same encoded string as in Android. How come the String.equals() returns "true" in java but "false" in android?
The error message from the server says "Invalid auth credentials". Does the encoded string differ between Android and Java for any reason? If yes, then point 2 makes sense.
I copied the encoded string from the java program into the Android variable and was able to authenticate successfully with shutterstock. So Indeed the encoded strings on Android and Java were different though in UTF-8 format. This also explains the "false" in Android and the "Invalid credentials" message from the server.
Just not sure why/how it differs when both the encoded strings are the same for the human eyes!
I've been trying to establish a connection with an API for more than a week now, to no avail. (Magic Card Market's, authentification documentation here and there). I'm supposed to receive a XML file.
I have what MCM call a "widget" access to their API, meaning that I don't have nor need a oauth_token (it's supposed to be an empty string) for the authorization header, and that I'm not supposed to receive nor use an access token/access secret.
The only things I do have are a consumer key (they call it app token sometimes) and a consumer secret.
Here is how I build my Authorization header :
private static String buildOAuthAuthorization(String method, String request)
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
String mkmAppToken = APICredentials.appToken;
String mkmAppSecret = APICredentials.appSecret;
String realm = "https://www.mkmapi.eu/ws/v1.1/games";
String oauthVersion = "1.0";
String oauthConsumerKey = mkmAppToken;
String oauthToken = "";
String oauthSignatureMethod = "HMAC-SHA1";
String oauthTimestamp = Long.toString(System.currentTimeMillis() / 1000);
String oauthNonce = Long.toString(System.currentTimeMillis());
String paramString = "oauth_consumer_key=" + oauthConsumerKey
+ "oauth_nonce=" + oauthNonce
+ "oauth_signature_method=" + oauthSignatureMethod
+ "oauth_timestamp=" + oauthTimestamp
+ "oauth_token=" + oauthToken
+ "oauth_version=" + oauthVersion;
String baseString = method + "&" + rawUrlEncode(realm) + "&" + rawUrlEncode(paramString);
String signingKey = rawUrlEncode(mkmAppSecret) + "&";
Mac mac = Mac.getInstance("HMAC-SHA1");
SecretKeySpec secret = new SecretKeySpec(signingKey.getBytes(), mac.getAlgorithm());
mac.init(secret);
byte[] digest = mac.doFinal(baseString.getBytes());
byte[] oauthSignature = Base64.encode(digest, Base64.URL_SAFE);
String authorizationProperty = "OAuth "
+ "realm=\"" + realm + "\", "
+ "oauth_version=\"" + oauthVersion + "\", "
+ "oauth_timestamp=\"" + oauthTimestamp + "\", "
+ "oauth_nonce=\"" + oauthNonce + "\", "
+ "oauth_consumer_key=\"" + oauthConsumerKey + "\", "
+ "oauth_token=\""+ oauthToken + "\", "
+ "oauth_signature_method=\"" + oauthSignatureMethod + "\", "
+ "oauth_signature=\"" + oauthSignature + "\"";
System.out.println(authorizationProperty);
return authorizationProperty;
}
The actual request is in an AsyncTask :
public static class oAuthRequest extends AsyncTask<String, Integer, StringReader> {
private int lastCode;
#Override
protected StringReader doInBackground(String... requestURLs) {
String method = requestURLs[0];
String url = requestURLs[1];
StringReader result = null;
try {
String authProperty = buildOAuthAuthorization(method, url);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.addRequestProperty("Authorization:", authProperty);
lastCode = connection.getResponseCode();
System.out.println("RESPONSE CODE 1 " + lastCode);
// Get content
BufferedReader rd = new BufferedReader(new InputStreamReader(lastCode == 200 ? connection.getInputStream() : connection.getErrorStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
result = new StringReader(sb.toString());
} catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
e.printStackTrace();
}
return result;
}
}
It seems like no matter what I change, I'm always getting a 401.
Things I've tried :
oauthSignature as a String using Base64.encodeToString()
Nonce generation using SecureRandom
With and without the empty oauthToken
Another timestamp generation method (can't remember what though)
signing key with and without app token (theorically I need only the consumer secret, but you never know)
Using HttpsURLConnection instead of HttpURLConnection (the URI start in https, so I thought, hey. But no)
At least 2-3 other different implementations (one who was basically a copy/paste of the Java example in the documentation of course -- it still kind of is one now)
(Probably a lot of things I can't even remember)
At this point I'm wondering if maybe the issue comes from my keys, as I've tried to use the Postman app to test requests with the same results.
I have a question about URI and URL
when i pass a url is work good but result is worst need help!!
as my code look like this.
import java.io.*;
import java.net.*;
import java.net.URL;
public class isms {
public static void main(String[] args) throws Exception {
try {
String user = new String ("boo");
String pass = new String ("boo");
String dstno = new String("60164038811"); //You are going compose a message to this destination number.
String msg = new String("你的哈达哈达!"); //Your message over here
int type = 2; //for unicode change to 2, normal will the 1.
String sendid = new String("isms"); //Malaysia does not support sender id yet.
// Send data
URI myUrl = new URI("http://www.isms.com.my/isms_send.php?un=" + user + "&pwd=" + pass
+ "&dstno=" + dstno + "&msg=" + msg + "&type=" + type + "&sendid=" + sendid);
URL url = new URL(myUrl.toASCIIString());
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Print the response output...
System.out.println(line);
}
rd.close();
System.out.println(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
the output in web is different..
on my java output is
你的哈达哈达!
but on my the site is
ÄãµÄ¹þ´ï¹þ´ï!
Help!!
String user = new String ("boo");
You don't need to (and shouldn't) do new String in Java—String user = "boo"; is fine.
String msg = new String("你的哈达哈达!");
Writing non-ASCII characters in your source means that you have to get the -encoding flag to javac to match the encoding you have saved your text files with. It is possible you have saved the .java file as UTF-8 but not configured your build environment to use UTF-8 at compile time.
If you are not sure that you've got this right, you can use ASCII-safe \u escapes in the meantime:
String msg = "\u4F60\u7684\u54C8\u8FBE\u54C8\u8FBE!"; // 你的哈达哈达!
Finally:
URI myUrl = new URI("http://www.isms.com.my/isms_send.php?un=" + user + "&pwd=" + pass
+ "&dstno=" + dstno + "&msg=" + msg + "&type=" + type + "&sendid=" + sendid);
When you're putting a URI together you should URL-escape each of the parameters you include in the string. Otherwise any & or other invalid character in the value will break the query. This also allows you to choose what charset is used to create the query string.
String enc = "UTF-8";
URI myUrl = new URI("http://www.isms.com.my/isms_send.php?" +
"un=" + URLEncoder.encode(user, enc) +
"&pwd=" + URLEncoder.encode(pass, enc) +
"&dstno=" + URLEncoder.encode(dstno, enc) +
"&msg=" + URLEncoder.encode(msg, enc) +
"&type=" + URLEncoder.encode(Integer.toString(type), enc) +
"&sendid=" + URLEncoder.encode(sendid, enc)
);
What the right value for enc is depends on the service you are connecting to, but UTF-8 is a good guess.
I need to put in my android applicazion a tool that uses the free service for sending free sms through internet...
I saw that many apps are able to integrate these sevices..
I tried a lot but I have not found anything useful..
So I ask you ... how can I use the gateway of uthsms.net (for example) for send SMS with my Android application?
Sorry for the generic question..but I not found any starting point for resolve this question.
Thanks in advance
Use a Tool like Firebug to see what gets sent when you click the button on the website. I see that a POST-Request is done to uthsms.net with some parameters. You should be able to do the same POST with your app.
These are the parameter:
button: Send SMS
country: (some integer)
gateway: 0
hyderabad: your message
remLen: remaining length??
sindh: number to send sms to (without the +)
x: some integer
y: some integer
To send this POST-request in Android use following code:
URL url = new URL("http://uthsms.net");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String data = URLEncoder.encode("button", "UTF-8") + "="
+ URLEncoder.encode("Send SMS", "UTF-8");
data += "&" + URLEncoder.encode("country", "UTF-8") + "="
+ URLEncoder.encode(country, "UTF-8");
data += "&" + URLEncoder.encode("gateway", "UTF-8") + "="
+ URLEncoder.encode("0", "UTF-8");
data += "&" + URLEncoder.encode("hyderabad", "UTF-8") + "="
+ URLEncoder.encode(message, "UTF-8");
data += "&" + URLEncoder.encode("remLen", "UTF-8") + "="
+ URLEncoder.encode(remLen, "UTF-8");
data += "&" + URLEncoder.encode("sindh", "UTF-8") + "="
+ URLEncoder.encode(number, "UTF-8");
data += "&" + URLEncoder.encode("x", "UTF-8") + "="
+ URLEncoder.encode("0", "UTF-8");
data += "&" + URLEncoder.encode("y", "UTF-8") + "="
+ URLEncoder.encode("0", "UTF-8");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader inStream = new BufferedReader(new InputStreamReader((conn.getInputStream())));
result = inStream.readLine();
inStream.close();
The result seems to be a html-document. Somewhere inside you should find the success message, or possible errors.
Try GoogleVoice API. It has documentation, which might help you get started.
Here's a link : https://code.google.com/p/google-voice-java/
I have the following code snippet that tries to make an HTTP call to my servlet:
try {
// Construct data
String data = URLEncoder.encode("rpt_type", "UTF-8") + "=" + URLEncoder.encode(reportType, "UTF-8");
data += "&" + URLEncoder.encode("rpt_project", "UTF-8") + "=" + URLEncoder.encode(reportProject, "UTF-8");
data += "&" + URLEncoder.encode("rpt_mrv_creator", "UTF-8") + "=" + URLEncoder.encode(reportMrvCreator, "UTF-8");
data += "&" + URLEncoder.encode("rpt_gi_recipient", "UTF-8") + "=" + URLEncoder.encode(reportGiRecipient, "UTF-8");
data += "&" + URLEncoder.encode("rpt_plant", "UTF-8") + "=" + URLEncoder.encode(reportPlant, "UTF-8");
data += "&" + URLEncoder.encode("rpt_sloc", "UTF-8") + "=" + URLEncoder.encode(reportStorageLoc, "UTF-8");
data += "&" + URLEncoder.encode("rpt_gi_no", "UTF-8") + "=" + URLEncoder.encode(reportGiNo, "UTF-8");
data += "&" + URLEncoder.encode("date_sap_gi_fr", "UTF-8") + "=" + URLEncoder.encode(reportDateGiFrom, "UTF-8");
data += "&" + URLEncoder.encode("date_sap_gi_to", "UTF-8") + "=" + URLEncoder.encode(reportDateGiTo, "UTF-8");
data += "&" + URLEncoder.encode("rpt_partno", "UTF-8") + "=" + URLEncoder.encode(reportPartNo, "UTF-8");
data += "&" + URLEncoder.encode("rpt_so_no", "UTF-8") + "=" + URLEncoder.encode(reportSvcOrderNo, "UTF-8");
data += "&" + URLEncoder.encode("date_scan_fr", "UTF-8") + "=" + URLEncoder.encode(reportDateScanFrom, "UTF-8");
data += "&" + URLEncoder.encode("date_scan_to", "UTF-8") + "=" + URLEncoder.encode(reportDateScanTo, "UTF-8");
System.out.println("[data]\n" + data);
// Send data
String urlString = "http://localhost:8080/aerobook/GIStatusReportDownload?" + data;
System.out.println("[url] " + urlString);
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
//conn.setDoOutput(true);
//OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
//wr.write(data);
//wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
//wr.close();
rd.close();
} catch (Exception e) {
}
My debug output:
[data]
rpt_type=d&rpt_project=aaa&rpt_mrv_creator=bbb&rpt_gi_recipient=ccc&rpt_plant=ddd&rpt_sloc=eee&rpt_gi_no=fff&date_sap_gi_fr=02%2F05%2F2012&date_sap_gi_to=03%2F05%2F2012&rpt_partno=ggg&rpt_so_no=hhh&date_scan_fr=26%2F05%2F2012&date_scan_to=31%2F05%2F2012
[url] http://localhost:8080/aerobook/GIStatusReportDownload?rpt_type=d&rpt_project=aaa&rpt_mrv_creator=bbb&rpt_gi_recipient=ccc&rpt_plant=ddd&rpt_sloc=eee&rpt_gi_no=fff&date_sap_gi_fr=02%2F05%2F2012&date_sap_gi_to=03%2F05%2F2012&rpt_partno=ggg&rpt_so_no=hhh&date_scan_fr=26%2F05%2F2012&date_scan_to=31%2F05%2F2012
On my servlet (in a separate file from the code above), I generate an Excel file for download:
res.setContentType(sContentType);
res.setHeader("Content-Disposition", "attachment;filename=\"" + sExcelFileName + "\"");
OutputStream oOutStrm = res.getOutputStream();
wbBook.write(oOutStrm);
oOutStrm.close();
My issue here is that from the URL generated by my code (as shown in the debug output above), I can access my servlet and I manage to get the Save-As dialog.
I'd like to get the contents of the file generated for use within my code. Is there any way I can get the attachment from my code, in byte stream or any other format?
Edit #3: Cleaned up the top
When you enter the URI into the browser, you are doing a GET request. Your client Java code however produces a POST request, and sends the parameters in the body, not the URI.
You may want to look at an HTTP trace and compare.
I want to get the contents of the excel file on my code, but so far it's not working.
I find no errors in the code.
I believe you want to convert content from input stream to an HSSFWorkbook object.
Following code snippet will help you on it.
java.net.URLConnection conn = url.openConnection();
java.io.InputStream is = conn.getInputStream();
org.apache.poi.hssf.usermodel.HSSFWorkbook workBook = new org.apache.poi.hssf.usermodel.HSSFWorkbook( is );
System.out.println( "Number of Sheets: " + workBook.getNumberOfSheets() );
org.apache.poi.hssf.usermodel.HSSFSheet sheet = workBook.getSheetAt( 0 );
System.out.println( "Sheet Name: " + sheet.getSheetName() );
// rest of your code to handle the captured workBook
// cheers
Check wbBook.write(oOutStrm); whether anything has been written into outputStream, also you need call oOutStrm.flash() before close it.
I doubt that the problem lies at OutputStream oOutStrm = res.getOutputStream();.
I bet res is HttpServletResponse and it returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data
Check API
So, You might not be getting anything except fileName.
In Servlet Try
FileOutputStream stream = new FileOutputStream("c:/excel/Book1.xls");
workBook.write(stream);