2012-07-23 45 views
0

荫创建自动化系统发布数据的形式登记到网站如何发布数据,以形成在其他的服务器

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

我认为上述POSTDATA只是要求属性,相应编码。但仔细检查视图源后,我才知道这些是表单属性。 我不能访问该表单。现在我怎样才能将数据发布到表单中,以便用户通过网站进行注册?

我必须将值设置为formbean。 请提供您的建议。

+0

当你运行该代码你什么反应? – Santosh 2012-07-26 12:13:29

+0

获取状态代码为500 – developer 2012-07-26 14:58:44

+1

做一件事。安装[Firebug](http://getfirebug.com/)。你将需要firefox。现在从浏览器提交该表单并观察网络流量。查找帖子请求。这会让你知道所有提交给服务器的字段。 – Santosh 2012-07-26 15:04:15

回答

0

我推荐使用Apache HttpClient。它更快,更容易实施。

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. 

有关详细信息,你可以参考http://hc.apache.org/

+0

你确定将值设置为formbean吗? – developer 2012-07-23 08:49:12

2

你正在使用您的POST错误Content-Type:你需要使用application/x-www-form-urlencoded。一旦你改变了这一点,服务器会将你的请求体解释为请求参数,并且(很可能)你的“formbean”将被填充数据。

上面的代码可能是一个测试用例,但您真的应该注意正确编码您尝试POST的所有数据。否则,您可能会面临语法无效的请求(在这种情况下,服务器将拒绝请求或忽略重要参数),或者引入一个安全漏洞,用户可以在POST中注入任意请求参数。我强烈推荐的代码看起来像这样:

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

看来愚蠢编码的静态字符串,如“userNameConfirm”,但如果你进入这个习惯,你最终会使用它所有的时间和你的代码会更安全。

此外,你需要确保你通过OutputStream发送数据有权Content-Length:你计算内容长度正常,但你不使用你用于计算的字节发送给客户。你希望你的代码看起来更象这样:

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

你可以找到在社区维基一个非常全面的HTTPUrlConnection教程:Using java.net.URLConnection to fire and handle HTTP requests

+0

也许用* postData.append(“&"); *”)替换* postData.append(“&”); *以使其完美 – 2012-08-01 17:10:35

+0

@ e-sushi'&'和'='明确允许作为x-www中的分隔符-forma-urlencoded,不应该被转义,另外,“&”是一个XML实体,它是URL编码的错误编码类型; URL编码使用百分比编码的十六进制数字代替 – Alex 2012-08-01 17:40:34

+0

@Alex Oops,你是对的。我忘了RFC 3986的细节。我的坏,抱歉,不会因为非法访问企图而改变他的Error 500问题;) – 2012-08-01 17:56:07

0

如果你的项目使用Spring 3.x或更高,我会建议使用Spring RestTemplate对于做http非常方便,下面的代码将会记录下来。

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

您在评论中描述的“HTTP错误500”是“内部服务器错误”。

这意味着服务器无法使用您的请求(GET/POST),或者您尝试呼叫的服务器存在特定的问题。

考虑看看你调用的URL,我立刻相同的错误500

同样的情况,两个GET和POST httqs请求://www.walmart.com/subflow/YourAccountLoginContext/1471476370/sub_generic_login/CREATE_ACCOUNT。(停用实时链接;将“q”替换为“p”以使其工作)。

总之:一般来自WallMart服务器的返回“HTTP Error 500”阻止您的呼叫成功。

顺便说一句:

的情况并不少见,以得到一个错误,而不是500 403,如果他们被锁定您的访问了。

由于您可能没有拥有WallMart网站,并且您尝试访问其网站的价值受到第三方访问保护的级别,所以很可能是这种情况。 ;)

PS:我不确定明智地显示AccountLogin号码是否明智这样。毕竟,这是一个特定WallMart账户持有人的客户ID。但是,嘿,那是你的选择,不是我的。

0

此外,仔细检查您发送的参数。服务器正在进行的输入数据可能会有一些验证。例如,某些字段是强制性的,有些字段只是数字等。

0

尝试通过修改用户代理作为浏览器进行欺骗。 WalMart可能有一个安全机制,可以自动检测你是否正在做这件事。

(如果你有问题设置用户代理看到这个帖子:Setting user agent of a java URLConnection

相关问题