开发者

Server returned HTTP response code: 500 with HttpUrlConnection in Android

I am Posting some data into a specific URL by using the following code:

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());
         }

}}

It sucessfully posts, but when I retrieve the data, it gives me the following error:

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)

I am using the following code:

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));

            S开发者_StackOverflowtring data;

            while ((data = reader.readLine()) != null) 
            {
                 System.out.println(data);
            }
            reader.close();

    }
    catch(Exception e)
    {
        System.out.println(" Excpetion = " + e);
        e.printStackTrace();        
    }
}}

Can anyone tell me where I am wrong in my code? Any suggestions.


  1. Localhost is accessible as you are able to do the post call.
  2. The error code 500 indicates that your sever is not able to handle the request, due to any reason. main be some exception or URL is not mapped.
  3. Incase of exception read the ErrorStream method instead of Inputstream to know about the error Example connection.getErrorStream()


By seeing your code i think You are trying to read data from your localHost If want connection to your localhost you have to specify IP Address of your computer for example

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜