私が開発しているアプリでは、サードパーティAPIにデータを投稿する必要があります。私は初めから認証に苦労し続けており、さらに進んでいきました。そして今、私は立ち往生しています。POSTでBasic Authが失敗しましたが、C#で動作します
私はAuthenticator
を使用して試してみましたが、すべての特定のAndroidのバージョンにバグがあるように見えるかについて読んだことがある:Authentication Example
ノー成功とApache Commons HTTP Library
など、いくつかの異なるオプションを試してみました。この後、APIが苦労していないことを確認することにしました。だから私は速いWinForms
プログラムを書いてAPIをテストしました。これは最初の試みで完全に機能しました。だから私が働いているアイデアと両方で作業しているAPIはうまく見えますが、なぜJavaコードが機能していないのかに関するいくつかのガイダンスが切望されています。毎回作品
C#コード:
例に従う、私はこれは私のせいであると考えているものの、内部サーバーエラー:現在、500を返すAndroid用
System.Net.ServicePointManager.Expect100Continue = false;
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(addWorkoutUrl);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "distance=4000&hours=0&minutes=20&seconds=0&tenths=0&month=08&day=01&year=2011&typeOfWorkout=standard&weightClass=H&age=28";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.Headers["X-API-KEY"] = apiKey;
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("username:password"));
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
MessageBox.Show(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
MessageBox.Show(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
Javaコード。
URL url;
String data = "distance=4000&hours=0&minutes=20&seconds=0&tenths=0&month=08&day=01&year=2011&typeOfWorkout=standard&weightClass=H&age=28";
HttpURLConnection connection = null;
//Create connection
url = new URL(urlBasePath);
connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(10000);
connection.setUseCaches(false);
connection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible) ");
connection.setRequestProperty("Accept","*/*");
connection.setRequestProperty("X-API-KEY", apiKey);
connection.setRequestProperty("Authorization", "Basic " +
Base64.encode((username + ":" + password).getBytes("UTF-8"), Base64.DEFAULT));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes("UTF-8").length));
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(data.getBytes("UTF-8"));
wr.flush();
wr.close();
statusCode = connection.getResponseCode();
statusReason = connection.getResponseMessage();
//At this point, I have the 500 error...
さらに調査したところ、私は最終的に問題がどこにあるように見えたのか、まだ解決する方法については助けが必要だとわかりました。 AndroidでのBase64エンコーディングはC#と大きく違うようですが、エンコードをスキップしてC#からエンコードされた文字列をコピーすれば、すべて動作します。 誰かが私がBase64エンコーディングで間違っていることを提案していますか? – gtcompscientist