标签: httpurlconnection

Java HttpUrlConnection POST 请求特殊字符奇怪的行为

我正在尝试使用 HttpURLConnection 实现 POST 请求。这是我的代码:

\n\n
private static void call(String body) throws IOException{\n    HttpURLConnection con = null;\n\n    con = (HttpURLConnection)new URL("http://127.0.0.1:8080").openConnection();\n\n    con.setRequestProperty("Accept-Charset", "UTF-8");\n    con.setRequestMethod("POST");\n    con.setRequestProperty("Content-Type", "application/json; charset=utf-8"); \n    con.setRequestProperty("Accept", "application/json; charset=utf-8");\n\n    con.setDoOutput(true);\n    DataOutputStream wr = new DataOutputStream(con.getOutputStream());\n    wr.writeBytes(body);\n    wr.flush();\n    wr.close();\n    ...\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我将其发布到本地主机只是为了用 WireShark 嗅探它。\n问题是,当我的正文是包含诸如 \' \xc3\xb2 \' \' \xc3\xa0 \' \' \xc3\xa8 \'之类的字符的字符串时\' \xc3\xa7 \' ...我看到的请求的字符串正确,这些字符被点替换。

\n\n

示例:\nif 正文是“ h\xc3\xa8llo! ” ---> 请求正文是“ h.llo!

\n\n

只是为了测试,我在 java main 中执行上述方法,并以这种方式传递参数:

\n\n
String pString = "{\\"titl\xc3\xa8\\":\\"H\xc3\xa8llo W\xc3\xb2rld!\\"}";\nString …
Run Code Online (Sandbox Code Playgroud)

java post http utf-8 httpurlconnection

2
推荐指数
1
解决办法
6259
查看次数

改造和 HTTP 补丁

所以我想使用 Retrofit 发出 PATCH 请求,但目前我无法将 okhttp 添加到我的类路径中。当我尝试发出 PATCH 请求时,我得到下面的堆栈跟踪。有没有其他方法可以在不使用 okhttp 的情况下使用 Patch?

\n\n
    java.net.ProtocolException\n        at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:644)\n        at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:50)\n        at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)\n        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)\n        at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)\n        at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)\n        at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)\n        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)\n        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)\n        at retrofit.Platform$Android$2$1.run(Platform.java:142)\n        at java.lang.Thread.run(Thread.java:1019)\n06-09 10:53:09.349    1809-1897/**.****.****** D/Retrofit\xef\xb9\x95 ---- END ERROR\n
Run Code Online (Sandbox Code Playgroud)\n

android patch httpurlconnection retrofit

2
推荐指数
1
解决办法
2607
查看次数

Android - 从服务器下载文件被终止

我们正在尝试从服务器下载一个将近 7MB 的 apk 文件。从输入流中读取数据时,流正在终止。我们没有收到任何错误消息。下面是我们累了的代码。

   URL url = new URL(versionUpgradeModel.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setConnectTimeout(3 * 60 * 1000);
connection.connect();

long totalBytes = 0;

// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    inputStream = connection.getInputStream();
    totalBytes = connection.getContentLength();
}

String filePath = localFilePath + "/TestApp.apk";
CWTSLog.e("FILE", "CREATED" + localFilePath);
OutputStream outputStream = null;
try

{
    // write the inputStream to a FileOutputStream
    outputStream = …
Run Code Online (Sandbox Code Playgroud)

java android httpurlconnection

2
推荐指数
1
解决办法
1023
查看次数

如何检查 url 方案是 Http 还是 Https 并根据它创建 URLConnection

所以基本上现在我的应用程序配置为使用 https,因为在“版本”中它将使用自签名证书,并且显然也使用 https。

我当前的测试系统(还有一些功能)不使用 https,而是使用 http。我认为最好有某种类型的方法来检查给定的 URL 是 Http 还是 Https,并根据结果创建正确的 URLConnection。

我当前的问题是我不知道该方法到底应该是什么样子。我考虑过在连接到我的服务器的方法中使用 if 语句,但可能有更好的解决方案。

如有帮助,将不胜感激。

android httpurlconnection xamarin

2
推荐指数
1
解决办法
1261
查看次数

如何使用列android在ListView中显示对象数组?

我开发了混合 android 应用程序,我现在正在学习本机 android。我想在原生 android 中创建一个简单的表格视图。现在在离子我这样做。

JS

$scope.get_users = function(){
   $http.post('192.168.0.132/api/get_users', {is_active:true})
     .success(function(data){
         $scope.users = data;
     }.error(function(Error_message){
         alert(Error_message)
     }
}
Run Code Online (Sandbox Code Playgroud)

HTML

<table>
    <thead>
        <tr>
           <th>Name</th>
           <th>Role</th>
        </tr>
    <tbody>
        <tr ng-repeat="user in users">
          <td ng-bind="::user.name"></td>
          <td ng-bind="::user.role"></td>
          <td><button ng-click="select_user(user)"></td>
        </tr>
     </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

简单吧?一切都已就绪。

现在在android中这是我的代码。

public void get_users(){
    HttpURLConnection urlConnection;
    int length = 5000;

    try {
       // Prepare http post
       URL url = new URL("http://192.168.0.132/api/get_users");
       urlConnection = (HttpURLConnection) url.openConnection();
       urlConnection.setRequestProperty("Authorization", "");
       urlConnection.setRequestMethod("POST");
       urlConnection.setDoInput(true);
       urlConnection.setDoOutput(true);
       urlConnection.setUseCaches(false);
       urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

       // initiate http post call …
Run Code Online (Sandbox Code Playgroud)

android listview httpurlconnection

2
推荐指数
1
解决办法
1049
查看次数

java中的HTTPS和HTTP连接

我知道 HTTPS 扩展了 http。那么这是否意味着我可以做到这一点?

HttpUrlConnection connect = passmyurl.openconnection(url);
Run Code Online (Sandbox Code Playgroud)

HttpsUrlConnection connect = passmyurl.openconnection(url);



public static HttpsURLConnection passmyurl(URL url) throws IOException {
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        return connection;
    }
Run Code Online (Sandbox Code Playgroud)

这是否意味着两者都会起作用?由于HTTps扩展了HTTP,这意味着我也可以将HTTP url传递给这个函数吗?

java url http httpurlconnection

2
推荐指数
1
解决办法
5111
查看次数

Android 中的 HttpURLConnection 工作正常,但 OkHttp 在 Kitkat 上显示“网络无法访问”

我对这个特定网站有问题:https : //tastedive.com/read/api

如果我使用 HttpURLConnection 执行 HTTP 请求,我会得到一个正常的 HTML 响应(在 Android 上,此代码需要位于一个单独的线程中,并且还进行了所有必要的尝试和捕获):

    StringBuilder result = new StringBuilder();
    URL url = new URL("https://tastedive.com/read/api");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    System.out.println(result.toString());  // shows normal HTML response
Run Code Online (Sandbox Code Playgroud)

但是如果我用 OkHttp 来做,用这个代码......

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url("https://tastedive.com/read/api")
            .build();
    client.newCall(request).enqueue(new okhttp3.Callback() {
        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
            Log.d("MY", …
Run Code Online (Sandbox Code Playgroud)

android httpurlconnection okhttp android-4.4-kitkat

2
推荐指数
1
解决办法
2200
查看次数

Android 2.3.3互联网适用于我的Galaxy S但不适用于我的Galaxy Tab?(权限)

我有这个奇怪的问题.我正在检索twitters,它可以在模拟器和我的三星Galaxy S上运行,但它在我的Galaxy Tab 10.1上不起作用?

手机和标签上都安装了相同的应用程序.从Eclipse生成所以没有调试或任何东西.

需要不同的权限?

这是代码:

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);

ResponseHandler<String> responseHandler = new BasicResponseHandler();

responseBody = client.execute(get, responseHandler);
Run Code Online (Sandbox Code Playgroud)

这是显而易见的:

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.INTERNET"  />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)

java android httpurlconnection ui-thread android-4.0-ice-cream-sandwich

1
推荐指数
1
解决办法
347
查看次数

不推荐使用readLine()要改变什么?

我是Java初学者,试图让它在Eclipse中运行.但是,它readLine已被删除并且通知说它已被弃用.代码工作,虽然不是while ((var2 = var5.readLine()) != null) { 一点......所以我想知道如何解决它.

final class ScreenShotHelper$1 implements Runnable
{
    public void run()
    {
        try
        {
            String var1 = ScreenShotHelper.access$000().getAbsolutePath();
            String var2 = "";
            HttpURLConnection var3 = null;
            DataOutputStream var4 = null;
            DataInputStream var5 = null;
            String var6 = "\r\n";
            String var7 = "--";
            String var8 = "*****";
            String var9 = "";
            int var10 = 1048576;
            String var11 = "";
            var9 = Minecraft.getMinecraft().thePlayer.username;
            String var12 = "http://localhost/screenupload/index.php?playername=" + var9;
            try
            {
                FileInputStream var13 …
Run Code Online (Sandbox Code Playgroud)

java httpurlconnection datainputstream

1
推荐指数
1
解决办法
1万
查看次数

Android HttpURLConnection设置GET Request方法

我想用GET方法发送HTTP请求,但我无法设置GET方法.这是我的代码:

try {
                URL url = new URL(path);
                conn = (HttpURLConnection) url.openConnection();

                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
                        .appendQueryParameter("p1", "123")
                        .appendQueryParameter("p2", "123");
                String query = builder.build().getEncodedQuery();
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(query);
                writer.flush();
                writer.close();
                os.close();

                conn.connect();
                Log.e("ERROR", conn.getResponseMessage());
                Log.e("ERROR", conn.getRequestMethod());
                Log.e("ERROR", String.valueOf(conn.getResponseCode()));

            } catch (Exception e) {
                Log.e("ERROR", e.getMessage());
            }
Run Code Online (Sandbox Code Playgroud)

在代码中,我设置了GET方法,但是在日志中,请求方法是POST:

02-01 16:48:54.766  23799-23831/? E/ERROR? Method Not Allowed
02-01 16:48:54.766  23799-23831/? E/ERROR? POST
02-01 16:48:54.766  23799-23831/? E/ERROR? 405
Run Code Online (Sandbox Code Playgroud)

有什么问题?

android httprequest httpurlconnection

1
推荐指数
1
解决办法
2212
查看次数