You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用PHP接口返回301 Moved Permanently错误,求助解决方法

Fixing 301 Moved Permanently Error When Calling PHP API

Hey there, let's work through this 301 redirect issue you're facing. A 301 response means the server is telling you the URL you're requesting has permanently moved to a new location—most likely, your http:// address is being redirected to https:// (a super common security practice). Let's break down solutions for both your HttpURLConnection code and Volley requests.

First, Confirm the Redirect Target

Before jumping into code changes, let's verify where the redirect is pointing. Run this command in your terminal:

curl -v http://myappdemo.com/payumoney/payUmoneyHashGenerator.php

Look for the Location header in the response—you'll almost certainly see something like https://myappdemo.com/payumoney/payUmoneyHashGenerator.php. That's your new target URL.

Solution 1: Fix HttpURLConnection Code

You have two straightforward options here:

Option A: Use the HTTPS URL Directly

This is the most efficient fix, since it skips the extra redirect request entirely. Just update your URL to use https:// instead of http://, and you can remove the setInstanceFollowRedirects(false) line (or set it to true to handle future redirects automatically):

// Replace http with https in your URL
URL url = new URL("https://myappdemo.com/payumoney/payUmoneyHashGenerator.php");
String postParam = postParams[0];
byte[] postParamsByte = postParam.getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Remove this line or set to true: conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Content-Length", String.valueOf(postParamsByte.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postParamsByte);

// Rest of your response handling code stays the same
InputStream responseInputStream = conn.getInputStream();
StringBuffer responseStringBuffer = new StringBuffer();
byte[] byteContainer = new byte[1024];
for (int i; (i = responseInputStream.read(byteContainer)) != -1; ) {
    responseStringBuffer.append(new String(byteContainer, 0, i));
}
Log.e("tag", "doInBackground: "+ responseStringBuffer.toString());

Option B: Manually Handle the Redirect

If you want to keep the original URL and handle redirects programmatically, check the response code and re-send the request to the new location:

URL url = new URL("http://myappdemo.com/payumoney/payUmoneyHashGenerator.php");
String postParam = postParams[0];
byte[] postParamsByte = postParam.getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postParamsByte.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postParamsByte);

// Check for redirect response
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
    String newUrl = conn.getHeaderField("Location");
    // Re-initialize connection to the new URL
    conn.disconnect();
    conn = (HttpURLConnection) new URL(newUrl).openConnection();
    // Re-apply all request settings
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(postParamsByte.length));
    conn.setDoOutput(true);
    conn.getOutputStream().write(postParamsByte);
}

// Proceed with reading the response
InputStream responseInputStream = conn.getInputStream();
// ... rest of your code

Solution 2: Fix Volley Request

Volley should handle 301 redirects by default, but it often fails if the redirect is from http to https. The easiest fix is again to use the HTTPS URL directly:

String url = "https://myappdemo.com/payumoney/payUmoneyHashGenerator.php";

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
        response -> {
            // Handle successful response here
            Log.e("tag", "Volley response: " + response);
        },
        error -> {
            // Handle error here
            Log.e("tag", "Volley error: " + error.getMessage());
        }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        // Add your POST parameters here
        Map<String, String> params = new HashMap<>();
        params.put("your_param_key", postParams[0]); // Adjust based on your actual params
        return params;
    }
};

// Add the request to Volley's queue
Volley.newRequestQueue(getApplicationContext()).add(stringRequest);

If you still run into issues, double-check that your app has internet access permission—make sure your AndroidManifest.xml includes <uses-permission android:name="android.permission.INTERNET"/>.


内容的提问来源于stack exchange,提问作者Bharat Kumar Emani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:27:34