我正在努力使用一些Java代码,该代码在我的本地主机上正确执行时工作正常,但在通过我的Web服务器执行时发生的HTTP响应406失败。我试图读取406错误代码,但是,我坚持代码在本地正常工作的事实。代码的目的是通过HTTP向Web服务器发送请求以执行后端PHP程序。 HTTP请求不需要参数。 PHP程序从SQL数据库中检索一些数据,并通过JSON将其返回给Java程序。在本地运行时,这一切都可以正常工作(例如通过“localhost”)。下面的代码的最新咒语......Java到PHP - 返回JSON但收到HTTP 406响应
// preparation
String charset = "UTF-8";
String szURL = new String();
int responseCode = 0;
if (!debugConfiguration)
szURL = "https://secure.justhost.com/xxxx.php"; // removed actual file name
else
szURL = "http://localhost/xxxx.php"; // removed actual file name
// HTTP Post request
URL object = null;
try
{
object = new URL(szURL);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//xxxHttpsURLConnection con = null;
HttpURLConnection con = null;
try
{
//xxxcon = (HttpsURLConnection) object.openConnection();
con = (HttpURLConnection) object.openConnection();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//add request header
try
{
//xxxcon.setRequestMethod("POST");
con.setRequestMethod("GET");
}
catch (ProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//con.setRequestProperty("Accept-Charset", charset);
//con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
//con.setRequestProperty("User-Agent", USER_AGENT);
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = null;
try
{
wr = new DataOutputStream (con.getOutputStream());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
wr.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
responseCode = con.getResponseCode();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine;
StringBuffer response = new StringBuffer();
try
{
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson();
jsonArticleCategories[] enums = gson.fromJson(response.toString(), jsonArticleCategories[].class);
for (int i=0; i<enums.length; ++i)
{
jsonArticleCategories jac = enums[i];
cats.add(jac.get_category());
}
正如你可以从代码中看到的,我试过的参数,等我可能失去了一些东西很基本的一些变化,但是,我只是没有看到它。也许我一直在盯着代码太久。如果有人能指出我的缺点,我将非常感激。提前致谢。
你确定现场服务器正在发送'application/json'内容类型以及有效的JSON吗? –
406表示服务器无论出于何种原因都发现请求不可接受。看到实际上被推到电线上会很有趣。顺便说一句:带有一个“Content-Type”头的GET请求有点奇怪......也许你不应该无条件设置这个头部? – DaSourcerer
@ExplosionPills - 呃,我不确定。我忘了提及,当我通过输入java代码中的相同url来运行服务器上的PHP代码时,它确实可以正常工作。我看到了网页上返回的请求的json字段。 – rrirower