无法写入参数使用HttpURLConnection类给url [英] Unable to write the parameters to url using HttpURLConnection
问题描述
我想发布一个带参数的一个HTTP URL。我在追加使用
被跳过,控制出来的 appendQueryPrameters
语句的参数但是建立()的AsyncTask
。下面是个片段的的AsyncTask
私有类MyAsyncTask扩展的AsyncTask<字符串,整数,字符串> {
@覆盖
保护字符串doInBackground(字符串... PARAMS){
// TODO自动生成方法存根
串givenDob =参数[0];
串givensurname =参数[1];
字符串givenCaptcha =参数[2];
串响应=;
尝试{
Uri.Builder建设者=新Uri.Builder()
.appendQueryParameter(出生日期,givenDob)
.appendQueryParameter(userNameDetails.surName,givensurname)
.appendQueryParameter(CAPTCHA code,givenCaptcha);
查询字符串= builder.build()的toString()。
为PrintWriter了=的新PrintWriter(connection.getOutputStream());
的out.print(查询);
out.close();
INT响应code = connection.getResponse code();
Log.d(响应code,将String.valueOf(响应code)); / *的BufferedWriter作家=新的BufferedWriter(
新OutputStreamWriter(connection.getOutputStream(),ISO-8859-1)); writer.write(查询);
writer.flush();
writer.close();
* /
connection.getOutputStream()close()方法。
如果(响应code == HttpsURLConnection.HTTP_OK){
串线;
BR的BufferedReader =新的BufferedReader(新的InputStreamReader(connection.getInputStream()));
而((行= br.readLine())!= NULL){
响应+ =行; Log.d(回应,响应);
}
}其他{
响应=;
}
}赶上(IOException异常五){
e.printStackTrace();
}
返回响应;
} @覆盖
保护无效onPostExecute(String s)将{
Log.d(RES,S);
} }
我试着用的PrintWriter also.Still它的行查询字符串= builder.build()后跳过语句执行的toString();
PS:我已经在另外的AsyncTask打开 HttpURLConnection类
并调用对的onCreate()
下面是code。
网址URL =新的URL(https://myurl.com/path1/path2/path3.html);
连接=(HttpsURLConnection的)url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod(POST);
connection.setDoInput(真);
connection.setDoOutput(真);
用于 这 参考
URL strUrl =新的URL(https://myurl.com/path1/path2/path3.html?dateOfBirth =+参数[0] +
与& userNameDetails.surName =+参数[1] +
&放大器;验证码code =+参数[2]); Log.d(strUrl,将String.valueOf(strUrl)); 康涅狄格州的URLConnection = strUrl.openConnection();
[此code供应小改后的目的。但这个答案unforutnately OP删掉了他的评论。]
编辑:
其实我打开连接来获取验证码。使用上面的方法让我打开它让我错误的验证码错误另一个连接。所以这不是问题的答案。
EDIT2:
使用cookimanager帮助了我。
下面是更多
<一href=\"http://stackoverflow.com/a/35104167/5733855\">http://stackoverflow.com/a/35104167/5733855
I'm trying to post a an HTTP URL with parameters. I've appended the parameters using appendQueryPrameters
But statements after build()
are skipped and the control comes out of the AsyncTask
.Below is th snippet of the AsyncTask
private class MyAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String givenDob = params[0];
String givensurname = params[1];
String givenCaptcha = params[2];
String response = "";
try {
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("dateOfBirth", givenDob)
.appendQueryParameter("userNameDetails.surName", givensurname)
.appendQueryParameter("captchaCode", givenCaptcha);
String query = builder.build().toString();
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(query);
out.close();
int responseCode = connection.getResponseCode();
Log.d("responseCode", String.valueOf(responseCode));
/* BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(connection.getOutputStream(), "ISO-8859-1"));
writer.write(query);
writer.flush();
writer.close();
*/
connection.getOutputStream().close();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
Log.d("response", response);
}
} else {
response = "";
}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String s) {
Log.d("res", s);
}
}
I tried with PrintWriter also.Still it skips the execution of the statements after the line String query = builder.build().toString();
PS: I've opened the HttpURLconnection
in another AsyncTask and calling that on onCreate()
Below is the code.
URL url = new URL("https://myurl.com/path1/path2/path3.html");
connection = (HttpsURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Used this for reference
URL strUrl = new URL("https://myurl.com/path1/path2/path3.html?dateOfBirth=" + params[0] +
"&userNameDetails.surName=" + params[1] +
"&captchaCode=" + params[2]);
Log.d("strUrl", String.valueOf(strUrl));
URLConnection conn = strUrl.openConnection();
[This code serves the purpose after a small change. But unforutnately OP of this answer deleted his comment.]
Edit: Actually I'm opening connection to fetch the captcha . Using above method makes me open another connection which is getting me a wrong captcha error. So This is not the answer.
Edit2: Using cookimanager helped me. Here' more http://stackoverflow.com/a/35104167/5733855
这篇关于无法写入参数使用HttpURLConnection类给url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!