我目前正在 windows phone 上开发一个移动应用程序,它要求我向位于服务器上的 PHP 文件发送请求。
我遇到了一个问题,即请求在服务器上引发错误,但当我更改代码以分页本地 PHP 文件时却没有。任何想法为什么?
这是我的请求代码:
string sendString = "";
sendString += "&lat=" + (App.Current as App).mapCenter.Latitude;
sendString += "&lng=" + (App.Current as App).mapCenter.Longitude;
sendString += "&address=" + (App.Current as App).locationAddress;
if (checkNumber(CountryCode.Text, AreaCode.Text, Number.Text))
{
sendString += "&phone=" + CountryCode.Text + "" + AreaCode.Text + "" + Number.Text;
}
else
{
MessageBox.Show("Phone number invalid");
}
if (checkEmail(email.Text))
{
sendString += "&email=" + email.Text;
}
else
{
MessageBox.Show("Invalid Email");
}
sendString += "device=WindowsPhone";
sendString += (bool)prefsms.IsChecked ? "&contactMethod=sms" : "&contactMethod=email";
ListPickerItem selectedItem = (ListPickerItem)ServiceType.SelectedItem; //Cast listpickeritem into String
string content = (string)selectedItem.Content;
sendString += "&serviceType=" + content;
sendString += "&version=2.0";
sendString += "&language=" + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
(App.Current as App).watcher.Stop();
sendData = sendString;
if ((checkNumber(CountryCode.Text, AreaCode.Text, Number.Text)) && (checkEmail(email.Text)))
{
//Send data to server
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.BeginGetRequestStream(new AsyncCallback(RequestCallBack), myRequest);
}
else
{
}
private void RequestCallBack(IAsyncResult asyncresult)
{
HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest;
Stream dataStream = myRequest.EndGetRequestStream(asyncresult);
//Avoid trashing procedure
this.Dispatcher.BeginInvoke(delegate()
{
StreamWriter writer = new StreamWriter(dataStream);
writer.Write(sendData);
writer.Flush();
writer.Close();
//Expect a response
myRequest.BeginGetResponse(new AsyncCallback(ResponseCallBack), myRequest);
});
}
private void ResponseCallBack(IAsyncResult asyncresult)
{
HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncresult);
// Avoid trashing the procedure
this.Dispatcher.BeginInvoke(delegate()
{
// Retrieve response stream
Stream responseStream = myResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
// Show response
responseTxt = reader.ReadToEnd();
MessageBox.Show("" + responseTxt);
});
}
我正在向服务器发送一个字符串,服务器的PHP代码是这样的(测试的php完全一样):
require_once("includes/globals.php");
$vars = ($PRODUCTION)?$_POST:$_REQUEST;
$MINIMUM_VERSION = 2.0;
function validServiceType($type)
{
return $type == "taxi" || $type == "ladies" || $type == "limo" || $type == "handicap";
}
if(!isset($vars['version']) || ((float)$vars['version']) < $MINIMUM_VERSION)
{
echo "oldVersion";
}
elseif(isset($vars['lat'], $vars['lng'], $vars['phone'], $vars['language']))
{
/*do stuff */
if($data['contactMethod'] == "email" && $data['email'] == "")
{
echo "invalid";
die();
}
if(DB_AddHail($data))
echo "processing";
else if (($company = DB_GetReferralTaxi($data['lat'], $data['lng'], $data['serviceType'])))
{
/*do some more stuff*/
}
else
echo "sorry";
}
else
{
echo "invalid";
}
?>
我不断收到 OldVersion 错误,但我已经将版本号设置为 2.0...关于发生了什么的任何想法?
更新:我现在已经能够找出问题的根源;HttpWebRequest 根本没有向服务器发送任何数据;我的 $_POST 只是一个空数组。关于为什么的任何想法?
更新 2:我得到的错误是错误 302(“我使用的 URI 只是 http:// 而不是 https://)。但是在将协议更改为 https:// 后,它仍然不起作用。我的代码对那部分不正确吗?