服务器返回HTTP响应代码:Android中的HttpUrlConnection为500

时间:2011-07-09 10:49:54

标签: java android httpurlconnection

我使用以下代码Post将一些数据URL转换为特定的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}}

有谁能告诉我我的代码在哪里错了?任何建议。

2 个答案:

答案 0 :(得分:1)

通过查看您的代码,我认为您正在尝试从localHost中读取数据 如果想要连接到本地主机,则必须指定计算机的 IP Address 例如

URL url = new URL("http://IPAddressOfComputer/Sfacgi/accounts/lookUpaccount");

答案 1 :(得分:1)

  1. 可以访问Localhost,因为您可以进行发布呼叫。
  2. 错误代码500表示您的服务器由于任何原因无法处理请求。主要是一些例外或URL未映射。
  3. 异常的异常读取ErrorStream方法而不是Inputstream来了解错误 例  connection.getErrorStream()