Java 从android调用asmx HTTP POST
我正在尝试从android发布HTTP。如果我要求在LINQPAD中使用该C#代码,它会起作用Java 从android调用asmx HTTP POST,java,c#,android,web-services,http,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
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();
}
}
}
我的方法是:
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);
}
私有类下载任务扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//在这里执行请求,这样就不会中断UI线程
试一试{
返回下载内容(参数[0]);
}捕获(IOE异常){
return“无法检索数据。URL可能无效。”;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//在这里你完成了任务
}
}
私有字符串下载内容(字符串myurl)引发IOException{
InputStream=null;
整数长度=500;
试一试{
URL=新URL(myurl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(10000/*毫秒*/);
conn.setConnectTimeout(15000/*毫秒*/);
conn.setRequestProperty(“SOAPAction”、“soap action”);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”、“应用程序/xml”);
conn.setDoInput(真);
连接();
int response=conn.getResponseCode();
Log.d(标记“响应为:”+响应);
is=conn.getInputStream();
//将InputStream转换为字符串
字符串contentAsString=convertInputStreamToString(is,长度);
返回contentAsString;
}最后{
如果(is!=null){
is.close();
}
}
}
公共字符串convertInputStreamToString(InputStream,int-length)引发IOException,UnsupportedEncodingException{
Reader=null;
读卡器=新的InputStreamReader(流,“UTF-8”);
字符[]缓冲区=新字符[长度];
读(缓冲区);
返回新字符串(缓冲区);
}
您对内容类型使用了两个不同的值,这可能是个问题?@0xDEADC0DE如果我使用text/xml;charset=utf-8
作为内容类型,那么不幸的是http 400。我还看到你正在用C#编写一个字符串作为内容,而你没有在Android上这样做。这会导致问题吗?@0xDEADC0DE该字符串是我期望从android获得的响应(我正在检索IMEI代码和ip等)。我不确定它是否会解决您的问题,但当您执行POST请求时,您可能需要添加conn.setDoOutput(true)
您正在对内容类型使用两个不同的值,也许这是一个问题?@0xDEADC0DE,如果我使用text/xml;charset=utf-8
作为内容类型,那么不幸的是http 400。我还看到你正在用C#编写一个字符串作为内容,而你没有在Android上这样做。这会导致问题吗?@0xDEADC0DE该字符串是我期望从android获得的响应(我正在检索IMEI代码和ip等)。我不确定它是否会解决您的问题,但在您执行POST请求时,您可能需要添加conn.setDoOutput(true)
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);
}