2017-08-11 87 views
1

我收到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(); 
} 
+1

您的網址是否需要任何cookie?由於郵差可能已經添加,但在你的http請求中,你必須。 – Anurag

+0

當匿名訪問身份驗證關閉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中拒絕 –

+0

它也顯示響應 – Lassie

回答

0

您可以檢查並添加 '用戶代理',當你發送的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"); 
0

試試這個代碼:

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(); 
     }