Subversion Repositories SmartDukaan

Rev

Rev 13889 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class UpdateSDIUsingPanel {
        private static DefaultHttpClient client = new DefaultHttpClient();
        public static void main(String... args) throws ClientProtocolException, IOException, InterruptedException{
                System.out.println("Inside main");
                UpdateSDIUsingPanel UDSI = new UpdateSDIUsingPanel();
                UDSI.handleLogin();
                System.out.println("Created UD Inventory object");
                UDSI.updateInventory(32L,"SDL689893640");
                System.out.println("After calling update inventory");

        }
        int updateInventory(long stock,String supc) throws ClientProtocolException, IOException, InterruptedException{
                HttpPost post = new HttpPost("http://seller.snapdeal.com/inventory/update");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                System.out.println("Calling update inventory: Supc: "+supc+ " to be update stock: "+ stock);
                nameValuePairs.add(new BasicNameValuePair("changedField",
                                "inventory"));
                nameValuePairs.add(new BasicNameValuePair("changedValue",
                                String.valueOf(stock)));
                nameValuePairs.add(new BasicNameValuePair("supc",
                                supc));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
                HttpResponse response = client.execute(post);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                        System.out.println("Response of Inventory Update: "+line);
                }
                Thread.sleep(1000);
                return 1;
        }

        public void handleLogin() throws ClientProtocolException, IOException{
                HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
                HttpResponse response = null;
                boolean response_not_null = true;
                BufferedReader rd = null;
                while(response_not_null){
                        try {
                                response = client.execute(get);
                        } catch (Exception e) {
                                e.printStackTrace();
                                continue;
                        }
                        try {
                                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                        } catch (Exception e1) {
                                e1.printStackTrace();
                                continue;
                        }
                        response_not_null = false;
                }
                String line = "";
                StringBuffer sb = new StringBuffer();
                try {
                        while ((line = rd.readLine()) != null) {
                                sb.append(line);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
                int i= sb.toString().indexOf("name=\"lt\" value=");
                char[] charArray = sb.toString().toCharArray();
                String lt = "";
                int j=0;
                for(j=i+16;j<=charArray.length;j++){

                        if(charArray[j]==' '){
                                break;
                        }
                }
                lt = sb.substring(i+17,j-1);
                System.out.println("LT VALUE " + lt);
                i= sb.toString().indexOf("name=\"execution\" value=");
                charArray = sb.toString().toCharArray();
                String ex = "";
                j=0;
                for(j=i+24;j<=charArray.length;j++){
                        if(charArray[j]==' '){
                                break;
                        }
                }
                ex = sb.substring(i+24,j-1);
                System.out.println("EXECUTION VALUE " + ex);
                HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                //nameValuePairs.add(new BasicNameValuePair("username",
                //"khushal.bhatia@saholic.com"));
                nameValuePairs.add(new BasicNameValuePair("username",
                "saholic-snapdeal@saholic.com"));
                //nameValuePairs.add(new BasicNameValuePair("password",
                //"sonline"));
                nameValuePairs.add(new BasicNameValuePair("password",
                "saholic15"));
                nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
                nameValuePairs.add(new BasicNameValuePair("execution",ex));
                nameValuePairs.add(new BasicNameValuePair("lt",lt));
                nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
                response = client.execute(post);
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                line = "";
                while ((line = rd.readLine()) != null) {
                        System.out.println(line);
                }
                get = new HttpGet("http://seller.snapdeal.com/inventory/");
                response = client.execute(get);
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null) {
                        System.out.println(line);
                }
        }




}