为什么第一次没有建立连接?

发布于 2024-11-30 09:47:40 字数 1033 浏览 1 评论 0原文

我想发送我的 ID 和向服务器输入密码并获取服务器的响应。这是我的代码。这不是第一次工作。但是,如果我第二次执行我的应用程序,我会收到服务器的响应。它第一次抛出“Post 方法失败:-1 null”。我哪里错了??为什么 if() 块是第一次执行?你能告诉我吗?

HttpsURLConnection con = null;
String httpsURL = "https://www.abc.com/login";
String query = "id=xyz&password=pqr";
URL url = new URL(httpsURL); 
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent","Mozilla/4.0(compatible; MSIE 5.0; Windows 98; DigExt)"); 
con.setDoInput(true);
con.setDoOutput(true);  
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();  
int respCode = con.getResponseCode();

if (respCode != HttpsURLConnection.HTTP_OK) 
{
  throw new Exception("POST method failed: " + con.getResponseCode()+ "\t" + con.getResponseMessage()); }
 else {
//read the content from server
}

I want to send my id & password to server and get the response from server. Here is my code. It is not working for the first time. But iam getting the response from server if i execute my application on second time. It is throwing "Post method failed: -1 null" on first time. Where iam wrong?? Why if() block is executing on first time?? could you please tell me.

HttpsURLConnection con = null;
String httpsURL = "https://www.abc.com/login";
String query = "id=xyz&password=pqr";
URL url = new URL(httpsURL); 
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent","Mozilla/4.0(compatible; MSIE 5.0; Windows 98; DigExt)"); 
con.setDoInput(true);
con.setDoOutput(true);  
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();  
int respCode = con.getResponseCode();

if (respCode != HttpsURLConnection.HTTP_OK) 
{
  throw new Exception("POST method failed: " + con.getResponseCode()+ "\t" + con.getResponseMessage()); }
 else {
//read the content from server
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧街凉风 2024-12-07 09:47:40

1/ 建议使用 apache HttpClient 而不是 URLConnection (参见 http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html)

2/ 对于登录名和密码,为什么不使用 Http 身份验证? Android 支持基本版和摘要版。

3/至于你的问题,你没有关闭底层的outputStream。

你应该这样做:

OutputStream os = con.getOutputStream();
DataOutputStream output = new DataOutputStream(os);
output.writeBytes(query);
output.close();
os.close();

1/ It is recommanded to use apache HttpClient rather than URLConnection (see http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html)

2/ for login and password, why not use Http Authentication ? both basic and digest are supported by android.

3/ as for you problem, you don't close the underlying outputStream.

you should do:

OutputStream os = con.getOutputStream();
DataOutputStream output = new DataOutputStream(os);
output.writeBytes(query);
output.close();
os.close();
赠意 2024-12-07 09:47:40

使用其他技术和/或经典 java 检查服务器服务的有效性。您在问题中没有提到是否成功区分服务器和问题。

如果无法从响应中识别出任何代码(即响应不是有效的 HTTP),则来自 java doc ...getResponseCode 返回 -1。

Java https post 请求示例: http:// /www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

尝试在查询状态之后而不是之前关闭输出流......这可能会有所帮助

Check Server service validity with other technology and/or classic java. You didn say in your question if you succeed to discriminate the server from the issue.

from java doc ...getResponseCode returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).

Java https post request example : http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

try to close your outputstream after querying the status and not before...that may help

雨落星ぅ辰 2024-12-07 09:47:40

以下是在 Android 中发送 POST 请求的方式,

HttpPost httpGet = new HttpPost(server + "/login?email="+username+"&password="+password);
DefaultHttpClient httpClient = new DefaultHttpClient();         
HttpResponse response = httpClient.execute(httpGet);

您可以使用以下方式读取响应:

response.getEntity().getContent()

Here is how you should send POST requests in Android

HttpPost httpGet = new HttpPost(server + "/login?email="+username+"&password="+password);
DefaultHttpClient httpClient = new DefaultHttpClient();         
HttpResponse response = httpClient.execute(httpGet);

You can read response using:

response.getEntity().getContent()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文