获取拒绝访问消息,同时呼吁HTTP POST服务
问题描述:
我收到401次拒绝访问消息,同时调用HTTP后service.In邮差得到的结果,但同时通过服务调用它显示访问被拒绝的消息。请任何人都帮我解决我的问题。获取拒绝访问消息,同时呼吁HTTP POST服务
这是我的代码。
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("https://site:[email protected]/devkoopicafenetecco-2/_search");
request.setHeader("Content-type", "application/json");
JSONObject myjson = new JSONObject();
myjson.put("from", 0);
myjson.put("size", 10);
StringEntity entity = new StringEntity(myjson.toString());
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json; charset=utf-8"));
request.setEntity(entity);
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
HttpResponse response = httpClient.execute(request);
InputStream instream = response.getEntity().getContent();
String tempresult = convertStreamToString(instream);
instream.close();
result = tempresult.toString();
} catch (Exception ex) {
Log.e("exception","ex"+ex);
ex.printStackTrace();
}
答
您可以检查并添加 '用户代理',当你发送的request.Like这样的:
url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("User-Agent","Pk");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("GET");
或
request.setheader("User-Agent","PK");
答
试试这个代码:
try {
String url = "https://site:[email protected]/devkoopicafenetecco-2/_search";
SSLContext sslContext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
CloseableHttpClient client= HttpClientBuilder.create()
.setSSLSocketFactory(sslsf)
.build();
HttpPost request = new HttpPost(url);
request.addHeader("Accept", " */*");
request.addHeader("User-Agent", "runscope/0.1");
request.addHeader("Accept-Encoding", "gzip, deflate");
request.addHeader("Authorization", "Basic c2l0ZTphYjRhY2E2NTIyODRkN2RjODdjYTFmMzQ3ZjJhYzQzMg==");
HttpResponse response = client.execute(request);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
} catch (Exception ex) {
Log.e("exception","ex"+ex);
ex.printStackTrace();
}
您的网址是否需要任何cookie?由于邮差可能已经添加,但在你的http请求中,你必须。 – Anurag
当匿名访问身份验证关闭Web服务应用程序,所有调用的应用程序必须进行任何请求之前提供凭据。默认情况下,Web服务客户端代理不会继承运行Web服务客户端应用程序的安全上下文的凭据。 https://community.nintex.com/community/build-your-own/nintex-for-office-365/blog/2015/05/14/o365-call-http-web-service-failed-unauthorized-access-在ios中拒绝 –
它也显示响应 – Lassie