1
我Post
通過使用下面的代碼荷蘭國際集團的一些數據到一個特定的URL
:服務器返回的HTTP響應代碼:500 HttpURLConnection的Android中
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class LookupPOST {
public static void main(String[] args) throws Exception
{
String accountlookup = "<account>"+
"<name>N*</name>"+
"<type></type>"+
"<accountaddress>"+
"<address></address>"+
"<state></state>"+
"<zip></zip>"+
"<city></city>"+
"<country></country>"+
"<county></county>"+
"</accountaddress>"+
"</account>";
URL url = new URL(" http://localhost:8080/Sfacgi/accounts/lookUpaccount");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("authenticate", "BBUSER1:BBUSER1");
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("pinno", "2526121F96");
connection.setRequestProperty("appversion", "BBV15");
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(accountlookup);
writer.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
System.out.println(" Success ");
}
else
{
System.out.println("Server returned HTTP error code :"+connection.getResponseCode());
}
}}
它成功地職位,但是當我檢索數據,它給我下面的錯誤:
Excpetion = java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/Sfacgi/accounts/lookUpaccount
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:88080/Sfacgi/accounts/lookUpaccount
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
at com.jp.look.LookupGet.main(LookupGet.java:31)
我使用下面的代碼:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class LookupGet
{
public static void main(String[] args) throws Exception
{
try
{
URL url = new URL("http://localhost:8080/Sfacgi/accounts/lookUpaccount");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("authenticate", "BBUSER1:BBUSER1");
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("pinno", "2526121F96");
connection.setRequestProperty("appversion", "BBV15");
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
int timeoutMs=500000;
connection.setConnectTimeout(timeoutMs);
connection.setReadTimeout(timeoutMs);
//InputStream is = url.openStream();
InputStream is1 = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is1));
String data;
while ((data = reader.readLine()) != null)
{
System.out.println(data);
}
reader.close();
}
catch(Exception e)
{
System.out.println(" Excpetion = " + e);
e.printStackTrace();
}
}}
任何人都可以告訴我我的代碼中哪裏出錯了嗎?有什麼建議麼。
如果你發送帶有瀏覽器的請求會發生什麼? GET請求一般工作嗎? – morja
您正在使用本地主機url可能無法連接,所以請先在設備瀏覽器上嘗試該網址。 – milind