我正试图在我的Android应用程序中从Apache HTTP转移到HttpUrlConnection。我被卡住了,我试着到处寻找,但我无法通过它。这是我正在尝试的。
以下是我的HTTP代码:
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等价物:
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
。我使用Charles
,标题相同。
当我尝试在BufferedWriter
而不是网址中添加查询参数时,我将网址更改为基本网址,如下所示:
URL url1 = new URL(baseUrl);
并将以下行添加到writer中,如下所示:
writer.write(modUrl)
当我这样做时,我得到500 Internal Server Error
。
在这两种情况下,我都会在IOException
行上获得FileNotFoundException
InputStream
。
关于如何解决这个问题的任何想法?
答案 0 :(得分:0)
您应该从成功的httpclient调用中转储标头,这样您就可以准确地知道正在使用OK请求发送哪些标头。
目前尚不清楚如何设置“授权”标题
尚不清楚将什么Json值设置到'StringEntity'中。
在尝试使用HttpUrlConnection之前,您应该确切地知道在一个好的调用中发送了什么(httpClient调用或Curl CLI调用)。然后在那里设置相同的头,将相同的JSON写入Connections的outputStream,你应该得到相同的结果。
两个标题的卷曲表达式以及curl.-d中的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