从android调用asmx HTTP POST

时间:2016-02-22 10:17:21

标签: java c# android web-services http

我正在尝试从Android发布HTTP。如果我要求在LINQPAD中使用该C#代码,它可以正常工作

void Main()
{
    string r = String.Format(@"<s:Body><TrackMobileApp xmlns=""soap action url"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><device>test</device><imei>test</imei><ipAddress>127.0.0.1</ipAddress><timeStamp>2016-02-17T17:32:00.5147663+04:00</timeStamp></TrackMobileApp></s:Body>", DateTime.Now.ToString("o"));

    HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("URL.asmx");
    wr.ProtocolVersion = HttpVersion.Version11;
    wr.Headers.Add("SOAPAction", "soap action");
    wr.Method = "POST";
    wr.ContentType = "text/xml;charset=utf-8";
    using (StreamWriter sw = new StreamWriter(wr.GetRequestStream()))
    {
        sw.Write(r);
    } 
    HttpWebResponse rs = (HttpWebResponse)wr.GetResponse();
    if (rs.StatusCode == HttpStatusCode.OK)
    {
        XmlDocument xd = new XmlDocument();
        using (StreamReader sr = new StreamReader(rs.GetResponseStream()))
        {
            xd.LoadXml(sr.ReadToEnd());
            xd.InnerXml.Dump();
        }
    }
} 

但是从android它给了我http 415.出了什么问题?

我在onCreate

中有这个
new DownloadTask().execute("URL.asmx");

我的方法是:

private class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        //do your request in here so that you don't interrupt the UI thread
        try {
            return downloadContent(params[0]);
        } catch (IOException e) {
            return "Unable to retrieve data. URL may be invalid.";
        }
    }

    @Override
    protected void onPostExecute(String result) {
        //Here you are done with the task
    }
}

private String downloadContent(String myurl) throws IOException {
    InputStream is = null;
    int length = 500;

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestProperty("SOAPAction", "soap action");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/xml");
        conn.setDoInput(true);
        conn.connect();
        int response = conn.getResponseCode();
        Log.d(TAG, "The response is: " + response);
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = convertInputStreamToString(is, length);
        return contentAsString;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[length];
    reader.read(buffer);
    return new String(buffer);
}

0 个答案:

没有答案