0

Iam creating automated system to post the data to the form for registering into the web site

    URL url = new URL("https://www.walmart.com/subflow/YourAccountLoginContext/1471476370/sub_generic_login/create_account.do");
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);
HttpsURLConnection  uc = (HttpsURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
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(); 

I thought that those above postdata are just request attributes , and coded accordingly. But after closely checking the view source, i came to know that those are form attributes. I dnt have access to that form. Now how can i post the data to the form, so that the user get registered by the site?

i have to set the values to formbean. Please provide your suggesions.

Community
  • 1
  • 1
developer
  • 9,116
  • 29
  • 91
  • 150
  • What response do you get when you run this code ? – Santosh Jul 26 '12 at 12:13
  • 1
    Do one thing. Install [Firebug](http://getfirebug.com/). U will need firefox for that. Now submit that form from the browser and observe the network traffic. Look for the post request. This will give you an idea about all the fields being submitted to the server. – Santosh Jul 26 '12 at 15:04

6 Answers6

2

Your are using the wrong Content-Type in your POST: you need to use application/x-www-form-urlencoded. Once you change that, the server will interpret your request body as request parameters and (likely) your "formbean" will be filled with the data.

The above code may be a test case, but you really ought to take care to properly encode all of your data that you are trying to POST. Otherwise, you run the risk of either having a syntactically invalid request (in which case, the server will either reject the request, or ignore important parameters) or introducing a security vulnerability where a user can inject arbitrary request parameters into your POST. I highly recommend code that looks like this:

import java.net.URLEncoder;

String charset = "UTF-8"; // Change this if you want some other encoding
StringBuilder postData = new StringBuilder();
postData.append(URLEncoder.encode("firstName", charset));
postData.append("=");
postData.append(URLEncoder.encode(xlsDataList.get(0)), charset);
postData.append("&");
postData.append(URLEncoder.encode("lastName", charset));
postData.append("=");
postData.append(URLEncoder.encode(xlsDataList.get(1), charset));
postData.append("&");
postData.append(URLEncoder.encode("userName", charset));
postData.append("=");
postData.append(URLEncoder.encode(xlsDataList.get(2), charset));
postData.append("&");
postData.append(URLEncoder.encode("userNameConfirm", charset));
postData.append("=");
postData.append(URLEncoder.encode(xlsDataList.get(3), charset));
postData.append("&");
postData.append(URLEncoder.encode("pwd", charset));
postData.append("=");
postData.append(URLEncoder.encode(xlsDataList.get(5), charset));
postData.append("&");
postData.append(URLEncoder.encode("pwdConfirm", charset));
postData.append("=");
postData.append(xlsDataList.get(6), charset));

It seems silly to encode the static strings like "userNameConfirm", but if you get into that habit, you'll end up using it all the time and your code will be a lot safer.

Also, you need to make sure that the data you send through the OutputStream has the right Content-Length: you are computing the content-length properly, but then you aren't using the bytes you used for the computation to send to the client. You want your code to look more like this:

byte[] postDataBytes = postData.getBytes(charset);
uc.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length));
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream outputStream = uc.getOutputStream();
outputStream.write(postDataBytes);
outputStream.flush();
outputStream.close();

You can find a very comprehensive HTTPUrlConnection tutorial in the community wiki: Using java.net.URLConnection to fire and handle HTTP requests

Community
  • 1
  • 1
Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Maybe replace that *postData.append("&");* with *postData.append("&");* to make it perfect. –  Aug 01 '12 at 17:10
  • @e-sushi '&' and '=' are explicitly allowed as delimiters in x-www-form-urlencoded and should not be escaped. Also, "&" is an XML entity which is the wrong kind of encoding for URL encoding; URL encoding uses percent-encoded hex digits instead. – Alex Aug 01 '12 at 17:40
  • @Alex Oops, you're correct. I forgot about the details of RFC 3986. My bad, sorry. Won't change his Error 500 problem due to illegal access attempts though. ;) –  Aug 01 '12 at 17:56
0

I recommend to use Apache HttpClient. its faster and easier to implement.

PostMethod post = new PostMethod("https://www.walmart.com/subflow/YourAccountLoginContext/1471476370/sub_generic_login/create_account.do");
    NameValuePair[] data = {
      new NameValuePair("firstName", "joe"),
      new NameValuePair("lastName", "bloggs")
    };
    post.setRequestBody(data);
    InputStream in = post.getResponseBodyAsStream();
    // handle response.

For details you can refer http://hc.apache.org/

Jason
  • 1,241
  • 8
  • 16
0

If your project uses Spring 3.x or later I would recommend using the Spring RestTemplate its pretty handy for doing http, code below will log do a form post.

public String login(String username, String password)
    {
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add(usernameInputFieldName, username);
        form.add(passwordInputFieldName, password);
            RestTemplate template = new RestTemplate(); 
        URI location = template.postForLocation(loginUrl(), form);
        return location.toString();
    }
ams
  • 60,316
  • 68
  • 200
  • 288
0

The "HTTP Error 500" you've described in your comment is an "Internal server error".

This means that the server either can't use your request (GET/POST) or there's a problem specific to the server you are trying to call.

Taking a look at the URL you're calling, I immediately the same Error 500.

Same happens for both GET and POST requests at httqs://www.walmart.com/subflow/YourAccountLoginContext/1471476370/sub_generic_login/create_account.do (Live link deactivated; replace "q" with "p" to make it work.)

In short: the generally returned "HTTP Error 500" from WallMart's servers prevents your call to succeed.

By the way:

It's not uncommon to get an error 500 instead of a 403 if they are locking your access down.

As you probably don't own the WallMart website and since you're trying to access levels of their websites that are worth to be protected from 3rd party acces, this might well be the case. ;)

PS: I'm not sure if it's wise to show the AccountLogin number in public like this. After all, it's the client ID of a specific WallMart account holder. But hey, that's your choice, not mine.

0

Also, double check the parameters you are sending. There may be some validations on input data the server is doing. Eg, some fields are mandatory, some are numbers only, etc.

ddb
  • 1,416
  • 11
  • 17
0

Try spoofing as a browser by modifying the User Agent. WalMart may have a security mechanism that detects that you are doing this in an automated way.

(If you have problems setting the user agent see this post: Setting user agent of a java URLConnection)

Community
  • 1
  • 1
redolent
  • 4,159
  • 5
  • 37
  • 47