我正在尝试访问HTTPS请求,但当我尝试从我的Android应用程序访问它时,它无法访问服务器,但同样的网址从chrome http客户端正常工作。
这是我的POST HTTP请求代码
public String POST(String url){
InputStream inputStream = null;
String result = "";
try {
String jsonreply;
StringBuilder builder = new StringBuilder();
HttpClient client = getNewHttpClient();
HttpPost httpPost = new HttpPost(url);
Log.d(TAG, "URLS"+url);
try {
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.d(TAG, "Status code"+statusCode);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
jsonreply = builder.toString();
return jsonreply;
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
代码总是返回statusCode 404(Not Found)。我非常确定网址是正确的,因为我在其他应用程序中也访问了相同的网址。 我不能提供原始网址,但假的但是类似的是这个
https://test.test.se/user_session.json?email=test.test%40gmail.se&password=>Test12345%21%40%23%24&brand=LGE&phonemodel=Nexus+5&os_version=4.4.4&os_type=Android
感谢您的帮助
答案 0 :(得分:2)
我在这里回答了类似的问题,不想重复,检查一下是否有效 same issue
答案 1 :(得分:0)
将您的网址传递给此功能..
public static String readFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line);
}
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return stringBuilder.toString();
}
结果以字符串格式返回。
答案 2 :(得分:0)
使用这一个...... 尝试{ HttpClient httpclient = new DefaultHttpClient();
HttpContext localcon=new BasicHttpContext();
HttpGet httpget=new HttpGet(url1);
HttpResponse response=httpclient.execute(httpget,localcon);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}