将BASE64发布到服务器

时间:2018-05-03 15:19:34

标签: java android post httpurlconnection

我正在尝试将一个带有BASE64的图像发送到服务器,并且总是得到408这个消息“请求正文不包含指定的字节数。得到13.140,预期88.461”。我怎么解决这个问题?

我尝试使用Retrofit,HttpURLConnection并得到了同样的错误。我认为这是app上的一些参数。

 Gson gson = new Gson();
                    String jsonParam = gson.toJson(enviarPlataforma);

                    URL url = new URL("");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");

                    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                    conn.setRequestProperty("Accept", "application/json");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setFixedLengthStreamingMode(jsonParam.getBytes().length);


                    Log.i("JSON", jsonParam.toString());
                    DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                    os.writeBytes(jsonParam);

                    os.flush();
                    os.close();

                    Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                    Log.i("MSG", conn.getResponseMessage());

                    conn.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }

2 个答案:

答案 0 :(得分:0)

必须采用UTF-8字节,并按原样发送。 DataOutputStream完全用于其他方面。由于它是一次写入,您可以简单地使用从连接获得的OutputStream。 BufferedOutputStream可能没有用处。

永远不需要在结束前冲洗。只需要在一条直立的续线上冲洗。

尝试资源会自动关闭,也会在抛出某些异常时关闭。

    Private Sub cmdRegister_Click(sender As Object, e As System.EventArgs) Handles cmdRegister.Click
    Dim status As MembershipCreateStatus
    Dim organization As New Org
    Dim employee As New Employee
    Dim description As New Description(25)
    Dim userMembership As MembershipUser
    Dim stringBuilder As New StringBuilder

    Try
        Membership.CreateUser( _
            txtUserName.Text, _
            txtPassword.Text, _
            txtEmail.Text, _
            "question", _
            "answer", _
            True, _
            status)

        If status.ToString = "Success" Then
            organization.GSTRate = 1
            organization.QSTRate = 1
            organization.AccountStatus = 2
            organization.Name = txtOrg.Text
            organization.Active = 1
            organization.OrgTypeID = cboType.SelectedValue
            organization.Create()
            organization = Nothing
            organization = New Org(txtOrg.Text)

            employee.Username = txtUserName.Text
            employee.OrgID = organization.ID

            employee.FirstName = txtFName.Text
            employee.LastName = txtLName.Text
            employee.Title = txtTitle.Text
            employee.Username = txtUserName.Text
            employee.IsAdmin = True
            employee.IsSupervisor = True
            employee.IsAccountant = False
            employee.IsAdvalorem = True
            employee.Email = txtEmail.Text
            employee.Phone = ""

            employee.Create()

            Roles.AddUserToRole(employee.Username, "Admin")

            userMembership = Membership.GetUser(txtUserName.Text)

            stringBuilder.Append(description.EnglishDescription)
            stringBuilder.Replace("(name)", employee.FirstName & " " & employee.LastName)
            stringBuilder.Replace("(OrgName)", organization.Name)
            stringBuilder.Replace("(username)", employee.Username)
            stringBuilder.Replace("you must activate your account", "you must <a href='https://www.advataxes.ca/login.aspx?action=activate&id=" + userMembership.ProviderUserKey.ToString + "&username=" + userMembership.UserName + "'>activate your account</a>")

            SendEmail(userMembership.Email, "Advataxes: Account created ", stringBuilder.ToString, Session("language"))
            Session("NewUserEmail") = userMembership.Email

            Response.Redirect("message.aspx?id=364")
        Else
            lblInvalidUserName.Visible = True
            If status.ToString = "DuplicateUserName" Then lblInvalidUserName.Text = "Username already exists"
        End If

    Catch ex As MembershipCreateUserException
        MsgBox(GetErrorMessage(ex.StatusCode))

    Catch ex As HttpException
        MsgBox(ex.Message)

    Finally
        userMembership = Nothing
        organization = Nothing
        employee = Nothing
        description = Nothing
    End Try
End Sub

答案 1 :(得分:0)

我解决了我的问题。我不知道为什么,但是当我使用Fiddler时,我收到了这条消息。

谢谢你们。