get - 使用 httpconnection 发送 HTTP GET 请求失败
问题描述
我的请求失败了。这在邮递员中有效,但是在尝试 java 时,我尝试的每一种方式都失败了。下面的示例代码以及我得到的响应。我可以使用 httpsurlconnection 和 outputstreamwriter 成功地将 HTTPPost 发送到此 Web 服务,但是当尝试将其转换为 GET 时,没有任何不在 URL 本身中的内容,它会失败。非常感谢任何帮助!
//print out the encoded values
data.addToLog( “sha256hex: “, sha256hex);
data.addToLog( “xauth: “, xauth);
StringBuilder sb = new StringBuilder();
String baseurl = “https://” + apihost + endpoint + “?personID=” + personid;
data.addToLog( “BaseURL: “, baseurl);
//print out the JSON search request
try {
URL myurl = new URL(null, baseurl , new sun.net.www.protocol.https.Handler() );
HttpsURLConnection con = (HttpsURLConnection )myurl.openConnection();
con.setSSLSocketFactory(new TSLSocketConnectionFactory());
con.setRequestProperty(“X-Timestamp”, tsparam);
con.setRequestProperty(“X-Nonce”, nonce64);
con.setRequestProperty(“X-Authorization”, xauth);
con.setRequestProperty(“X-Test-Insecure”, “true”);
con.setRequestMethod(method);
con.setDoOutput(true);
con.setRequestProperty(“Content-Type”, “application/json;charset=utf-8”);
int responseCode = con.getResponseCode();
data.addToLog(“Response Code= “, con.getResponseCode() +”: “+ con.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
//System.out.println(response.toString());
data.addToLog(“Response:”, response.toString() );
}
日志结果:
PersonDetailsLookup,custom,Response Code= ,200: OK
PersonDetailsLookup,custom,Response:,{“serviceModel”:{“errorCode”:{“description”:”Unexpected System Error””value”:”500″}”errorMessage”:”API Invocation Failure – Unknown Error””severity”:{“description”:”FATAL””value”:”3″}}}
解决方案
弄清楚了!它是内容类型,需要将其更改为文本而不是 json:
con.setRequestProperty(“内容类型”,“应用程序/文本;charset=utf-8”);
推荐阅读
- java - 从选择的数据中创建一个唯一的字符串(可反向工程)
- c - 通过使用 while 循环,C 程序未在整数计数 150 处退出的问题
- apache-spark - 如何在 pyspark groupby 上将 UDF 与 pandas 一起使用?
- python-3.x - BigQuery: RefreshError: ('invalid_grant: Invalid JWT Signature.', {'error': 'invalid_grant', 'error_description': 'Invalid JWT Signature.'})
- deep-learning - 是否可以在 ML Kit 的帮助下将我的自定义深度学习模型部署到 android/ios?如果是怎么办?
- firebase - Firebase 函数在我可以在 Express 中处理之前解析请求正文
- laravel - Laravel Homestead - 页面停止工作 ERR_ADDRESS_UNREACHABLE
- postgresql - 使用“include”和“where”过滤关系
- oracle - PLSQL 中的动态游标 - Oracle
- python - Ctypes 扩展 Python 并返回数组 (2D)