[英]Can't set “POST” request to HttpURLConnection object
我打算將satang api 服務器與java 連接起來。 在以下代碼中,object 無法設置“POST”請求。 我不知道原因。 請幫我。
public String placeLimitOrder(String amount,String pair,String price,String side) throws IOException, BadResponseException
{
Long lnonce=new Date().getTime();
String nonce=lnonce.toString();
String req="amount="+amount+"&nonce="+nonce+"&pair="+pair+"&price="+price+"&side="+side+"&type=limit";
String operation="orders/?"+req;
String signature=getSignature(req);
StringBuilder result = new StringBuilder();
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "TDAX-API "+this.key);
con.setRequestProperty("Signature",signature);
con.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty( "charset", "utf-8");
con.setRequestProperty("User-Agent", "java client");
con.setUseCaches( false );
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
System.out.println(con.getHeaderField("Allow"));
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
public String placeLimitOrder(String amount,String pair,String price,String side) throws IOException, BadResponseException
{
Long lnonce=new Date().getTime();
String nonce=lnonce.toString();
String req="amount="+amount+"&nonce="+nonce+"&pair="+pair+"&price="+price+"&side="+side+"&type=limit";
String operation="orders/";
String signature=getSignature(req);
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestProperty("Authorization", "TDAX-API "+this.key);
con.setRequestProperty("Signature",signature);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("User-Agent", "java client");
con.setUseCaches( false );
JsonObject obj=new JsonObject();
obj.addProperty("amount", amount);
obj.addProperty("nonce", nonce);
obj.addProperty("pair", pair);
obj.addProperty("price", price);
obj.addProperty("side", side);
obj.addProperty("type", "limit");
String json=obj.toString();
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
這就是我所發現的,我不能肯定地說這是每個人的答案,但希望它可以為許多其他人解決這個問題。
似乎 HttpsURLConnection 實例的內部“方法”參數與實例上的requestMethod getter 和 setter 不太相關。 如果您像我一樣恢復使用 android 工作室中的調試器,因為登錄 android 非常糟糕。 在左側,我有顯示 URLConnection 實例內容的調試檢查器。 在右邊我有“評估表達式”的結果
connection.getRequestMethod() //right side of image
令人沮喪的是,沒有任何明確的答案(或許多其他 android 問題),當您嘗試找到它們時,您經常會收到使用他人庫的邀請。 這不是理想的解決方案,因為在許多情況下使用其他人的庫需要安全審查。
希望這會有所幫助。 就我而言,一切正常,但由於那里的錯誤,服務器正在發回 BAD_REQUEST 響應。
我遇到了同樣的問題。 我用下面的方法解決了這個問題。 第一的。 將您的“內容類型”設置為“應用程序/json”。 並將請求參數解析為您的帖子正文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.