2013-12-09 51 views
0

我正在向某个大学项目的服务器发送一些信息。我遇到的问题是服务器只会查看POST请求,它不会解析GET请求,这很公平。从应用程序发送Android HTTP POST请求,但服务器将其视为GET请求

我遇到的问题是我发送一个httpPost请求,我使用内置的Android方法(请参阅下文)检查此请求,但是当它到达服务器时,它将它视为GET请求。

邮编:

 JSONObject auth = new JSONObject(); 

     auth.put("TEST", "TESTING"); 

     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httpPost = new HttpPost("http://xxxxxxxxxxxxx/upload.php"); 

     String meth = httpPost.getMethod(); 

     Toast checker = Toast.makeText(this, meth, Toast.LENGTH_LONG); 
     checker.show(); 

     String json = ""; 

     json = auth.toString(); 

     StringEntity se = new StringEntity(json); 

     httpPost.setHeader("User-Agent", "android app"); 
     httpPost.setEntity(se); 

     httpclient.execute(httpPost); 

支票吐司,显示值POST,这是正确的。

服务器日志显示这是一个GET请求。

xxxxxxxxxxx MY IP - - [09/Dec/2013:00:20:57 +0000] "GET /upload.php HTTP/1.1" 405 352 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0" 

编辑severname/ip的日志和代码。

任何想法?

+0

默认方法在服务器上是GET,可以请你把你的服务器代码?我猜的东西是搞砸了那里。 – Techfist

+0

如果你有服务器代码将有助于 – SalGad

+0

这是一个小组任务,所以我没有在这台计算机上的服务器代码,他们还没有上传到GIT,所以即时通讯不知所措:/但我想我可以让他们去看看这个问题。你会说这肯定是客户端吗? –

回答

1

使用此方法,我的朋友发布的数据服务器

public Boolean postDataWithURL(String url, String fileUrl, 
      ArrayList<NameValuePair> listParamsWithValues) { 
     boolean result = false; 
     try { 
      int TIMEOUT_MILLISEC = 100000; // = 10 seconds 
      HttpParams httpParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParams, 
        TIMEOUT_MILLISEC); 
      HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
      HttpClient client = new DefaultHttpClient(httpParams); 
      HttpPost request = new HttpPost(url); 

      if (fileUrl.equalsIgnoreCase("")) { 
       request.setEntity(new UrlEncodedFormEntity(listParamsWithValues)); 
      } else { 
       // System.out.println("file path "+fileUrl+" with actual path "+file); 
      } 
      // request.setEntity(new 
      // ByteArrayEntity(listParamsWithValues.toString().getBytes("UTF8"))); 
      HttpResponse response = client.execute(request); 
      String responseString = request(response); 
      // System.out.println(responseString); 
      if (responseString.toLowerCase().contains("1")) { 
       result = true; 
      } else { 
       result = false; 
      } 
     } catch (Exception e) { 
      // Some things goes Wrong 
      e.printStackTrace(); 
     } 
     return result; 
    }