帮我翻译为java代码 var http = (HttpWebRequest)WebRequest.Create(url); http.Accept = "*/*"; http.Method = "POST"; http.ContentType = "application/json; charset=utf-8"; TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); var currentTimeSeconds = Convert.ToInt64(ts.TotalSeconds); var signature = AESHelper.AesLocal(AppId + ApiId + currentTimeSeconds, AppKey); http.Headers["signature"] = signature; http.Headers["apiname"] = ApiId; http.Headers["appid"] = AppId; http.Headers["appkey"] = AppKey; http.ContentType = "application/json;charset=utf-8"; http.UserAgent = "PostmanRuntime/7.26.8"; Random rd = new Random(); var rdNum = rd.Next(100000, 1000000); var signStr = thirdId + "/n" + rdNum + "/n" + currentTimeSeconds + "/n" + key+"/n"; var sign = Md5Helper.Encrypt32(Base64Helper.EncodeBase64(Encoding.UTF8,signStr)); http.Headers["thirdId"] = thirdId; http.Headers["nonceStr"] = rdNum.ToString(); http.Headers["time"] = currentTimeSeconds.ToString(); http.Headers["sign"] = sign; byte[] bs = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); using (var reqStream = http.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } using (var wr = http.GetResponse()) { using (var reader = new StreamReader(wr.GetResponseStream(), Encoding.UTF8)) { var result = reader.ReadToEnd(); var json = (JObject)JsonConvert.DeserializeObject(result); if (json["success"] == null || json["success"].ToString().ToLower() != "true") { Logger.Log(MethodBase.GetCurrentMethod().Name, GetType().Name, 1, "获取广告点位失败:" + JsonConvert.SerializeObject(param)+"////"+result, LogLevel.Error); } return result; } }
时间: 2024-02-14 12:13:08 浏览: 153
HTTP_Test_HTTP_http_test_c#http_httppost_http://test._
下面是Java代码的翻译:
```
URL url = new URL(urlStr);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setDoInput(true);
http.setUseCaches(false);
http.setRequestProperty("Accept", "*/*");
http.setRequestProperty("Content-Type", "application/json; charset=utf-8");
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long currentTimeSeconds = Convert.ToInt64(ts.TotalSeconds);
String signature = AESHelper.AesLocal(AppId + ApiId + currentTimeSeconds, AppKey);
http.setRequestProperty("signature", signature);
http.setRequestProperty("apiname", ApiId);
http.setRequestProperty("appid", AppId);
http.setRequestProperty("appkey", AppKey);
http.setRequestProperty("User-Agent", "PostmanRuntime/7.26.8");
Random rd = new Random();
int rdNum = rd.nextInt(1000000) + 100000;
String signStr = thirdId + "/n" + rdNum + "/n" + currentTimeSeconds + "/n" + key+"/n";
String sign = Md5Helper.Encrypt32(Base64Helper.EncodeBase64(Encoding.UTF8,signStr));
http.setRequestProperty("thirdId", thirdId);
http.setRequestProperty("nonceStr", String.valueOf(rdNum));
http.setRequestProperty("time", String.valueOf(currentTimeSeconds));
http.setRequestProperty("sign", sign);
byte[] postData = data.toString().getBytes(StandardCharsets.UTF_8);
DataOutputStream wr = new DataOutputStream(http.getOutputStream());
wr.write(postData);
wr.flush();
wr.close();
int responseCode = http.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String result = response.toString();
JSONObject json = new JSONObject(result);
if (json.isNull("success") || !json.getString("success").toLowerCase().equals("true")) {
Logger.Log(MethodBase.GetCurrentMethod().getName(), getClass().getName(), 1, "获取广告点位失败:" + param.toString() + "////" + result, LogLevel.Error);
}
return result;
} else {
throw new Exception("HTTP请求失败,状态码:" + responseCode);
}
```
阅读全文