2015-02-03 2 views
0

Android 앱에서 Apache HTTP에서 HttpUrlConnection으로 이동하려고합니다. 나는 붙어있어 어디서나 볼려고했지만 나는 그것을 통과 할 수 없다. 여기 제가 시도하고있는 것이 있습니다. 내가 위를하려고하면HTTPPost 대 Android의 HttpUrlConnection POST

List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
    for (Entry<String, String> entry: parameters.entrySet()) { 
     nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 
    } 

String queryString = URLEncodedUtils.format(nvps, HTTP.UTF_8); 
String modUrl = baseUrl + "?" + queryString; 
HttpURLConnection urlConnection = null; 
try { 
    URL url1 = new URL(modUrl); 
    urlConnection = (HttpURLConnection) url1.openConnection(); 
    CookieManager cookieManager = new CookieManager(); 
    CookieHandler.setDefault(cookieManager); 
    cookieManager.getCookieStore().add(new URI(url), podCookie); 
    urlConnection.setConnectTimeout(60000); 
    urlConnection.setReadTimeout(60000); 
    urlConnection.setDoOutput(true); 
    urlConnection.setRequestMethod("POST"); 

    urlConnection.setRequestProperty("Authorization", <auth-header>); 
    urlConnection.setRequestProperty("Content-Type", "application/json"); 

    OutputStream os = urlConnection.getOutputStream(); 
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
    writer.write(<String JSON to send>); 
    writer.flush() 
    writer.close(); 
    os.close(); 

    InputStream is = new BufferedInputStream(urlConnection.getInputStream()); 
    String responseString = WebService.convertInputStreamToString(is); 

, 나는이 401 Unauthorized error를 얻을 :

HttpParams timeoutParams = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(timeoutParams, 60000); 
HttpConnectionParams.setSoTimeout(timeoutParams, 60000); 

DefaultHttpClient httpClient = null; 
httpClient = new DefaultHttpClient(timeoutParams); 
Cookie podCookie = getPodCookie(); 
    if (podCookie != null) { 
     httpClient.getCookieStore().addCookie(podCookie); 
    } 
HttpPost postMethod = null; 
postMethod.addHeader("Authorization", "<auth-header>"); 
try { 
    List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
    for (Entry<String, String> entry : parameters.entrySet()) { 
     nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 
    } 
    String queryString = URLEncodedUtils.format(nvps, HTTP.UTF_8); 
    String modUrl = url + "?" + queryString; 
    postMethod = new HttpPost(modUrl); 

StringEntity entity = new StringEntity(<String JSON to send>, HTTP.UTF_8); 
postMethod.setEntity(entity); 
postMethod.setHeader("Content-type", "application/json"); 
HttpResponse reply = httpClient.execute(postMethod); 

이것은 위의 코드의 HttpURLConnection의 동등한은 다음과 같습니다

다음은 내 HTTP 코드입니다. Charles을 사용 중이며 머리글은 같습니다.

URL url1 = new URL(baseUrl); 

와 같이, 라이터에 다음 줄을 추가합니다 :

이 같은 대신 내가 기본 URL의 URL을 변경하려면 URL의 BufferedWriter의 검색어 매개 변수를 추가하려고

writer.write(modUrl) 

이렇게하면 500 Internal Server Error이됩니다.

두 경우 모두 IOExceptionFileNotFoundException이고 InputStream 줄에 있습니다.

해결 방법에 대한 아이디어가 있으십니까?

답변

0

정상적인 httpclient 호출에서 헤더를 덤프하여 OK 요청으로 어떤 헤더가 전송되는지 정확히 알 수 있도록해야합니다.

그것의 당신이 "인증"헤더를 설정하는 방법을 명확하지

그 JSON 값이 'StringEntity'로 설정되고 있는지 명확하지 않다.

HttpUrlConnection을 사용하기 전에 올바른 호출 (httpClient 호출 또는 Curl CLI 호출)에서 전송되는 내용을 정확히 알아야합니다. 그런 다음 동일한 헤더를 설정하고 동일한 JSON을 Connections의 outputStream에 쓰면 같은 결과가 나옵니다. 네 개의 헤더 및 스위치 curl. 차원 값에서 JSON 엔티티

컬 식 ....

curl -v -X POST \ 
    -H "Content-type: application/json" \ 
    -H "Authorization: aValueforAuth" \ 
    -d '{"score":1337,"playerName":"Sean Plott",...}' \ 
    http://domain/path?parm1=$urlEncodedVal-1&parm2=$urlEncodedVal-2