我是Android应用程序开发的新手,我需要的是我有两个文本框用户名和密码,它会发布到服务器并使用php页面检查它,如果登录成功则转到下一个屏幕,否则显示一个msg框显示登录错误我该怎么办?
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://google.com");
EditText tw =(EditText) findViewById(R.id.EditText01);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
tw.setText(status);
} catch (ClientProtocolException e) {
tw.setText(e.toString());
} catch (IOException e) {
tw.setText(e.toString());
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Android程序,它通过POST与App Engine程序共享数据,所有这些都在java中.当我使用MultipartEntity POST时,appengine程序由于某些我无法弄清楚的原因而无法看到我的任何POST变量!
Appengine代码:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Output:");
String mRoutine = req.getParameter("Routine");
resp.getWriter().println("Routine: " + mRoutine);
String mPMode = req.getParameter("PMode");
resp.getWriter().println("PMode: " + mPMode);
}
Run Code Online (Sandbox Code Playgroud)
使用Android代码段:
HttpPost post = new HttpPost("http://XXX.appspot.com/Echo");
HttpClient client = new DefaultHttpClient();
post.setEntity(new UrlEncodedFormEntity(data,HTTP.UTF_8));
HttpResponse response = client.execute(post);
HttpEntity resmarkMessagesReadFrom = response.getEntity();
if (resmarkMessagesReadFrom != null) {
Result_STR= "Response:\n" + EntityUtils.toString(resmarkMessagesReadFrom);
mMSGBox.setText(Result_STR);
}
Run Code Online (Sandbox Code Playgroud)
失败的Android代码段:
HttpPost post = new HttpPost("http://XXX.appspot.com/Echo");
HttpClient client = new DefaultHttpClient();
MultipartEntity reqmarkMessagesReadFrom = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); …Run Code Online (Sandbox Code Playgroud) 好的,这是我在登录帐户时从Live HTTP Header获得的标题(只是一个示例)信息:
http://example.com/login.html
POST /login.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://example.com
Cookie: blahblahblah; blah = blahblah
Content-Type: application/x-www-form-urlencoded
Content-Length: 39
username=shane&password=123456&do=login
HTTP/1.1 200 OK
Date: Sat, 18 Dec 2010 15:41:02 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.2.14
Set-Cookie: blah = blahblah_blah; expires=Sun, 18-Dec-2011 15:41:02 GMT; path=/; domain=.example.com; HttpOnly
Set-Cookie: blah = blahblah; expires=Sun, 18-Dec-2011 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用用C编写的程序从html表单中检索POST数据.
目前我正在使用:
char *formdata = getenv("QUERY_STRING");
if(formdata == NULL) /* no data retrieved */
Run Code Online (Sandbox Code Playgroud)
这似乎与表单"GET"方法一起正常工作,但不适用于"POST"方法.如何检索POST数据?
<h1>Length required</h1>在向服务器提交帖子字符串时,我一直收到错误.
$cookie = "Secret cookie data here";
$searchData = array(
'__EVENTTARGET' => 'ctl00$main$btn',
'ctl00$main$tbMNr' => $_GET['smth'],
'ctl00$main$tbMb' => $_GET['smthElse'],
'__VIEWSTATE' => 'smthElseHere'
);
// Commenting this out, as suggested by user lonesomeday
//foreach ($searchData as &$elem) // This should not be necessary
// $elem = urlencode($elem);
// create a new cURL resource
$fields = http_build_query($searchData); // Assuming it's an array
if ($ch = curl_init("http://mysite.com"))
{
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); …Run Code Online (Sandbox Code Playgroud) 所有.我使用的是jquery版本1.6.2.我做这样的ajax调用:
var jqxhr = $j.post(myPHP, function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function(data) { alert(JSON.stringify(data)); })
.complete(function() { alert("complete"); });
Run Code Online (Sandbox Code Playgroud)
脚本,运行和显示错误,比完成.有些人可能会... myPHP有问题,但myPHP总是显示:
{"sayHi":"hihi"}
Run Code Online (Sandbox Code Playgroud)
所以,当我打电话时,我去萤火虫检查链接是否有任何问题..:它显示了状态为200的POST网址,这没关系.此外,我可以通过firebugs看到响应....但问题是....为什么jquery让我有一个错误......:这里是错误消息:
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
Run Code Online (Sandbox Code Playgroud) 在RESTful SOA中,假设我通过AJAX发出POST请求,但在请求超时之前我没有得到响应.进一步假设重新提交请求将是有害的.POST不是幂等的.例如,也许我正在发布银行转帐.如果我没有收到回复,我不知道服务器是否处理了请求.
假设我可以控制客户端和服务端,那么处理此问题的最佳做法是什么?
我最初的想法是在每个POST请求中包含一个nonce(即一个伪id;某种唯一标识符); 例如,也许是If-None-Match标头中的值.使用这种方法,客户端可以以编程方式重新发出具有相同伪id的超时请求,如果服务器包含重复值,则服务器可以拒绝它.
我是C#的新手,所以我想知道是否有人可以帮我解决这个问题.我正在尝试将HttpPost从Windows Phone 8发送到服务器.我找到了两个我想要结合的例子.
第一个是发送Http Post的示例(http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx).这个问题是Windows Phone 8不支持它.
第二个例子是使用BeginGetResponse(http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.net.httpwebrequest (v=vs.105 ) .aspx).这支持Windows Phone 8.
我需要将第二个示例转换为BeginGetRequestStream(),就像第一个示例一样.我会尝试自己解决这个问题,但如果有人已经知道如何做到这一点,我会在网上发帖.我相信这对其他WP8开发人员有帮助.
更新 我现在正试图从服务器获得响应.我已经开始了一个新问题.请点击此链接(Http Post获取Windows Phone 8的响应错误)
似乎Redirect :: guest('login')仅适用于GET请求.IE浏览器.它会将经过身份验证的用户重定向到原始目标URL(GET).
在有POST请求的情况下,在用户成功登录后,auth过滤器是否有办法继续POST到URL?
一个简单的例子:我想显示一个可供任何人查看的表单.点击提交按钮后,auth过滤器将启动,这将使访客进入登录页面.身份验证成功后,我希望继续提交请求(即POST请求).
我正在用Java从外部系统调用REST.如果有任何连接中断,如外部系统脱机,我需要一个侦听器来检测并执行相应的操作.如果我能用Java实现这一点,请告诉我.优选地在Java 8中.
如果我遇到任何这种情况,目前我没有任何例外.我目前有以下代码
Client client = ClientBuilder.newClient();
WebTarget target = client.target(HOSTNAME);
Invocation.Builder requestBuilder;
requestBuilder = target.path(URL).request(MediaType.APPLICATION_JSON)
.header(HEADER_AUTHORIZATION, AUTH_TOKEN);
Response response = null;
if (HTTP_POST.equalsIgnoreCase(method)) {
try{
response = requestBuilder.post(Entity.entity(message, MediaType.APPLICATION_JSON_TYPE));
}catch(Exception ex ){
ex.printStackTrace();
}
} else if (HTTP_GET.equalsIgnoreCase(method)) {
response = requestBuilder.get();
}
String executeMessage = null;
if(response != null){
if (response.getStatus() == 200) {
executeMessage = response.readEntity(String.class);
return new JSONObject(executeMessage);
} else {
executeMessage = response.readEntity(String.class);
final JSONObject status = new JSONObject();
status.put(STATUS_CODE, response.getStatus());
status.put(STATUS_INFO, response.getStatusInfo());
status.put("response", executeMessage); …Run Code Online (Sandbox Code Playgroud)