2011-07-09 386 views
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();   
    } 
}} 

任何人都可以告訴我我的代碼中哪裏出錯了嗎?有什麼建議麼。

+1

如果你發送帶有瀏覽器的請求會發生什麼? GET請求一般工作嗎? – morja

+1

您正在使用本地主機url可能無法連接,所以請先在設備瀏覽器上嘗試該網址。 – milind

回答

1
  1. 本地主機是因爲您可以進行郵政通話。
  2. 錯誤代碼500表示您的服務器由於某種原因無法處理請求。主要是一些例外或URL未映射。例外的
  3. 櫃面閱讀ErrorStream方法,而不是InputStream的瞭解錯誤 例 connection.getErrorStream()
1

通過觀察你的代碼我認爲你正試圖從你的localHost 讀取數據如果要連接到本地主機,你必須指定您的計算機 的IP Address例如

URL url = new URL("http://IPAddressOfComputer/Sfacgi/accounts/lookUpaccount"); 
相關問題