2012-06-19 255 views
0

我目前正在开发一个Windows Phone上的移动应用程序,它要求我向位于服务器上的PHP文件发送请求。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://之后,它仍然没有':T的工作是我的代码不正确的那部分

+0

这段代码是否与你的代码相同?' return $ type ==“taxi”|| $ type ==“女士们”|| $ type ==“limo”|| $ type == handicap“;'如果是,请尝试从结尾删除'''! – undone

+0

^耶逃跑障碍。 – Killrawr

+0

对不起,这是我的一个错字;障碍已经在实际的现场版 – docaholic

回答

0

你可以试试,立即行之前,如果(isset开始($瓦尔?![' 版本']),并补充说:

echo "version: " . $vars['version']; 
exit; 

见如果它向浏览器输出任何内容,可能是因为你使用的php版本不像$ vars =($ PRODUCTION)?$ _ POST:$ _ REQUEST;语法,你可能需要手动完成所以像

$vars['version']=$_POST['version']; 
$vars['somethingElse']=$_POST['somethingElse'] 

等等

+0

是的,我试过了,$ vars ['version']给了我“2.0”,所以我真的不明白为什么它不起作用 – docaholic

+0

然后我很抱歉地说我也不确定。我将您的确切代码复制并粘贴到现场制作服务器上,并且效果非常好...祝您好运!希望有人能得到更多的帮助。 – JoeTomato

+0

哈哈感谢您的帮助;我想我会尝试使用不同的方法(使用WebClient而不是HttpWebRequest)对其进行重新编码,并查看是否有所作为 – docaholic

0

确保您的5.4.3,并与的phpinfo运行脚本();去检查。尝试改变你是PHP/C#来接收/发送GET请求,而不是POST请求。

这样,您可以至少测试一下,看看您何时发送PHP脚本的请求,例如http://yourdomain.com/script.php?name=John+Smith&Age=23,如果您获得预期的结果。下载https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/以检查结果时从浏览器的角度看标题是如何工作的。

+0

是的,我也试过了,它给出了相同的结果 – docaholic

+0

@docaholic因为你的跑步关闭本地主机你有没有试过改变你的URL呢?从“http:// localhost /”到“http://WORKSTATION_GROUP.USERNAME.local/test.php”(打开命令提示符并运行ping localhost。)。有些东西告诉我你的C#代码找不到localhost,是C IDE中声明为环境变量的本地主机?也有GET请求设置你的内容类型为“text/html”,看看是否有帮助。 – Killrawr

+0

嗯......即使使用GET,我仍然会得到相同的错误。另外,服务器的设置只能处理POST请求;我知道服务器也可以正常工作,因为我有一个具有相同服务的网站,并且功能完美。 – docaholic

相关问题