1

I want to know what type of content the server accepts, and if I can post the data only in that format?

Update : Below is the code.

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class ExcelReader {

     @SuppressWarnings("unchecked")
        public static void main(String[] args) throws Exception {
            //
            // An excel file name. You can create a file name with a full 
            // path information.
            //
            String filename = "E:\\Registration Details.xlsx";

            //
            // Create an ArrayList to store the data read from excel sheet.
            //
            List sheetData = new ArrayList();

            FileInputStream fis = null;
            try {
                //
                // Create a FileInputStream that will be use to read the 
                // excel file.
                //
                fis = new FileInputStream(filename);

                //
                // Create an excel workbook from the file system.
                //
                XSSFWorkbook  workbook = new XSSFWorkbook(fis);
                //
                // Get the first sheet on the workbook.
                //
                XSSFSheet  sheet = workbook.getSheetAt(0);

                //
                // When we have a sheet object in hand we can iterator on 
                // each sheet's rows and on each row's cells. We store the 
                // data read on an ArrayList so that we can printed the 
                // content of the excel to the console.
                //
                Iterator rows = sheet.rowIterator();
                while (rows.hasNext()) {
                    XSSFRow row = (XSSFRow) rows.next();
                    Iterator cells = row.cellIterator();

                    List data = new ArrayList();
                    while (cells.hasNext()) {
                        XSSFCell cell = (XSSFCell) cells.next();
                        data.add(cell);
                    }

                    sheetData.add(data);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    fis.close();
                }
            }

            showExcelData(sheetData);
        }

        private static void showExcelData(List sheetData) {
            //
            // Iterates the data and print it out to the console.
            //
            ArrayList xlsDataList = new ArrayList();
            for (int i = 0; i < sheetData.size(); i++) {
                List list = (List) sheetData.get(i);
                for (int j = 0; j < list.size(); j++) {
                    XSSFCell cell = (XSSFCell) list.get(j);
                    if(cell.getCellType()==cell.CELL_TYPE_STRING){
                        xlsDataList.add(cell.getRichStringCellValue().getString());
                   // System.out.print(
                          //  cell.getRichStringCellValue().getString());
                    }else  if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){
                        xlsDataList.add(cell.getNumericCellValue());
                        //System.out.print(
                              //  cell.getNumericCellValue());
                        }
                    if (j < list.size() - 1) {
                    //    System.out.print(", ");
                    }
                }
              //  System.out.println("");
            }
            if(null!=xlsDataList && xlsDataList.size()>0){
                authenticatenewPostUrl(xlsDataList);
            }
            for(int i=0;i<xlsDataList.size();i++){
                System.out.println(xlsDataList.get(i));
            }
        }


        public static void authenticatenewPostUrl(ArrayList xlsDataList) {

            HostnameVerifier hv = new HostnameVerifier() {

                @Override
                public boolean verify(String urlHostName, SSLSession session) {
                    System.out.println("Warning: URL Host: " + urlHostName
                            + " vs. " + session.getPeerHost());
                    return true;
                }
            };
            // Now you are telling the JRE to trust any https server.
            // If you know the URL that you are connecting to then this should
            // not be a problem
            try {
                trustAllHttpsCertificates();
            } catch (Exception e) {
                System.out.println("Trustall" + e.getStackTrace());
            }
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            try {

                URL url = new URL("https://www.walmart.com/cservice/ya_index.gsp");
                 String line;

                    StringBuilder string=new StringBuilder();
                    HttpsURLConnection  ucc = (HttpsURLConnection) url.openConnection();
                    InputStream content = (InputStream) ucc.getInputStream();
                    BufferedReader in = new BufferedReader(new InputStreamReader(content));
                    while ((line = in.readLine()) != null) {
                     string.append(line);

                    }
                 Document document=Jsoup.parse(string.toString());
                    Elements elements=document.getElementsByTag("a");//("mainSpriteSliderBTN wmBTN_blue25 continue");
                    int count=0;
                    //String registrationURL="";
                    String herfURL="";
                    for(Element elem:elements)
                    {
                        Elements e=elem.getElementsByAttributeValueMatching("class", "mainSpriteSliderBTN wmBTN_blue25 continue");
                        if(e!=null && !e.toString().isEmpty())
                        {
                          System.out.println(e.get(0).toString()+"\n"+count++);
                          Element myElem=e.get(0);
                          System.out.println(myElem.attr("href"));
                          herfURL=myElem.attr("href");
                        }
                    }
                    URL registrationURL = new URL("https://www.walmart.com"+herfURL);

                //String credentials = "ptt" + ":" + "ptt123";
                String postData = "firstName="+xlsDataList.get(0)+"&lastName="+xlsDataList.get(1)+"&userName="+xlsDataList.get(2)+"&userNameConfirm="+xlsDataList.get(3)+"&pwd="+xlsDataList.get(5)+"&pwdConfirm="+xlsDataList.get(6);
               // String encoding = Base64Converter.encode(credentials.getBytes("UTF-8"));
                HttpsURLConnection  uc = (HttpsURLConnection) registrationURL.openConnection();
                uc.setDoInput(true);
                uc.setDoOutput(true);
              //  uc.setRequestProperty("Authorization", String.format("Basic %s", encoding));
                uc.setRequestMethod("POST");
                uc.setRequestProperty("Accept", "*/*");
                uc.setRequestProperty("Content-Length", Integer.toString(postData.getBytes().length));
                uc.setRequestProperty("Content-Type", "text/html; charset=utf-8");

                OutputStreamWriter outputWriter = new OutputStreamWriter(uc.getOutputStream());
                outputWriter.write(postData);
                outputWriter.flush();
                outputWriter.close();
                System.out.println(uc.getResponseCode()+":::"+uc.getResponseMessage());

            } catch (MalformedURLException e) {
                e.printStackTrace();
                pw.println("Invalid URL");
            } catch (IOException e) {
                e.printStackTrace();
                pw.println("Error reading URL");
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(sw.toString());
        }

        public static class TempTrustedManager implements
        javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    public boolean isServerTrusted(
            java.security.cert.X509Certificate[] certs) {
        return true;
    }

    public boolean isClientTrusted(
            java.security.cert.X509Certificate[] certs) {
        return true;
    }

    public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType)
            throws java.security.cert.CertificateException {
        return;
    }

    public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType)
            throws java.security.cert.CertificateException {
        return;
    }
}

private static void trustAllHttpsCertificates() throws Exception {

    // Create a trust manager that does not validate certificate chains:

    javax.net.ssl.TrustManager[] trustAllCerts =

    new javax.net.ssl.TrustManager[1];

    javax.net.ssl.TrustManager tm = new TempTrustedManager();

    trustAllCerts[0] = tm;

    javax.net.ssl.SSLContext sc =

    javax.net.ssl.SSLContext.getInstance("SSL");

    sc.init(null, trustAllCerts, null);

    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(

    sc.getSocketFactory());

}

}

I thought that data is been posted to above url, but When i print the response code and response message iam getting as 500:::Internal Server Error

When i check this link internal server error 500

I came to know we have send data to server, i.e accepted type only.

If not please correct me and how can i resolve the above issue.

Community
  • 1
  • 1
developer
  • 9,116
  • 29
  • 91
  • 150
  • 1
    @downvoters, please comment on downvoting – developer Jul 20 '12 at 01:19
  • 2
    Yeah what does your question mean? – Mitch Connor Jul 20 '12 at 01:20
  • I second this. Your post is very spartan, and you might want to consider supplying the important back-story information such as what is motivating you to check this out and what problems you're currently having. – Hovercraft Full Of Eels Jul 20 '12 at 01:22
  • iam trying to post the data, i want to know what kind of data does the server accepts.Mean, we will be setting for parameter content-type. so what kind of data can i send to particualr server – developer Jul 20 '12 at 01:22
  • 1
    Why do you think the problem is related to the request's content type? The HTTP response for a wrong content type is 415 Unsupported Media Type. A 500 Internal Server Error response from a Java server usually means an exception was thrown, and that could be caused by anything. – Wyzard Jul 20 '12 at 01:34
  • 1
    You've posted a lot of code. Please reduce it to the *pertinent* content (the part that actually relates to the question you're asking). Expecting us to wade through all those lines of code to try and figure out what you're asking is a little unreasonable. Please edit to include a small section of code you can ask specific questions about, instead of asking people to try and guess. – Ken White Jul 20 '12 at 01:36

1 Answers1

1

iam trying to post the data, i want to know what kind of data does the server accepts.Mean, we will be setting for parameter content-type. so what kind of data can i send to particualr server

You don't get the content-type from the server, you get it from the form that you POST from, or it's pre-negotiated (like in documentation). There isn't a method that you can request to get what content-type a particular resource on the server wants in a POST body.

If you are submitting a form from, say, an HTML page, by default it's just application/x-www-form-urlencoded, otherwise the <FORM> tag will tell you what the content-type needs to be (like multipart/form-data).

Looking at your edited post, it looks like you're POSTing data, that's application/x-www-form-urlencoded:

postData = "firstName="+xlsDataList.get(0)+"&lastName="+xlsDataList.get(1)+
    "&userName="+xlsDataList.get(2)+"&userNameConfirm="+xlsDataList.get(3)+
    "&pwd="+xlsDataList.get(5)+"&pwdConfirm="+xlsDataList.get(6);

But you are telling the server that it's text/html:

            uc.setRequestProperty("Content-Type", "text/html; charset=utf-8");

Which is clearly wrong. No idea if this is what's causing your 500 error response, though. Conclusion: you have to know what content-type the request you are request wants its data in. There's no general way to query the server and ask.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • The default Content-Type for a submitted HTML webform is `application/x-www-form-urlencoded` unless explicitally specified otherwise in the form's [`enctype`](http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-enctype) attribute. – Remy Lebeau Jul 20 '12 at 01:58
  • @TylerHeiks: HTML and HTTP do not have reflection. – Remy Lebeau Jul 20 '12 at 01:59
  • @RemyLebeau yes I know, but he said he has access to the server locally. If he doesn't have source he can at least reflect on the JVM on his server. – Mitch Connor Jul 20 '12 at 16:10