我无法连接到网络服务http请求失败err400

时间:2017-03-08 00:56:30

标签: asp.net database web-services

我确实使用SQL Server Management Studio Express(免费版)来创建我的数据库,Visual Studio社区(免费版)通过ASP.net创建Web服务SOAP(当你点击它时,WS只有1个web方法然后显示5个空字段),然后我通过解决方案配置菜单中的选项Debug在VS中运行我的WS,之后页面打开,我确实检查了我的方法在我的数据库中插入数据,直到这里一切正确,数据是插入适当的列。

我的问题从构建Android应用程序开始,应用程序有5个空字段,用户将填充,然后将信息发送到WS并将数据放入数据库,但显示下一个错误:
                                                             org.ksoap2.transport.HttpResponseException:HTTP请求失败,HTTP状态:400
在org.ksoap2.transport.HttpTransportSE.call

我读了关于这个问题的每一个帖子,但没有一个信息解决了我的问题,我试图制作另外一个项目有3个不同的变种,但没有一个工作,我改变了localhost为我的本地IP,使用10.0.2.2:端口,我在末尾加上“?wsdl”等等。

我使用SOAPUI连接来测试我的WS,但是当我尝试从任何位置连接时都说我找不到页面。然后我意识到,也许我的应用程序不起作用,因为WS只适用于我和其他任何人,并且需要将我的WS放在一个真实的域或类似的东西。

我在这个世界上非常棒,但我真正的目标是我的应用程序可以在互联网上的数据库中获取/添加数据,以便用户可以查看/添加数据,我不知道这种方式对于这个目标是否正确。

欢迎任何建议,请我在3天前搜索信息,我无法解决这个问题。

提前致谢!

我的Android代码如下:

package com.example.ejembd2;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {


private TextView txtResultado;
private Button btnins;
private EditText txtTit;
private EditText txtDes;
private EditText txtAco;
private EditText txtPal;
private EditText txtFec;
private TextView txtResultado2;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCodigo = (EditText) findViewById(R.id.txtReg);
    txtNombre = (EditText) findViewById(R.id.txtVal);
    txtResultado = (TextView) findViewById(R.id.txtResultado);

    btnInsertar = (Button) findViewById(R.id.btnInsertar);
    btnActualizar = (Button) findViewById(R.id.btnActualizar);
    btnEliminar = (Button) findViewById(R.id.btnEliminar);
    btnConsultar = (Button) findViewById(R.id.btnConsultar);

    btnins = (Button) findViewById(R.id.InsEnBD);
    txtTit = (EditText) findViewById(R.id.T);
    txtDes = (EditText) findViewById(R.id.D);
    txtAco = (EditText) findViewById(R.id.A);
    txtPal = (EditText) findViewById(R.id.P);
    txtFec = (EditText) findViewById(R.id.F);
    txtResultado2 = (TextView) findViewById(R.id.txtResultado2);

    btnins.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String a = txtTit.getText().toString();
            String b = txtDes.getText().toString();
            String c = txtAco.getText().toString();
            String d = txtPal.getText().toString();
            String f = txtFec.getText().toString();
            new insercion1().execute(a, b, c, d, f);
        }
    });
}



private class insercion1 extends AsyncTask<String,Integer,Boolean> {


    @Override
    protected Boolean doInBackground(String... params) {

        String a = params[0];
        String b = params[1];
        String c = params[2];
        String d = params[3];
        String f = params[4];

    boolean resul = true;

    final String NAMESPACE = "http://proof.net/";
    /*final String URL = "http://10.0.2.2:50490/ServicioWebSoap/ServicioClientes.asmx"; //cambio*/
        final String URL = "http://10.0.2.2:50490/ServicioClientes.asmx";
    final String METHOD_NAME = "NuevoEvento";
    final String SOAP_ACTION = "http://proof.net/NuevoEvento";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("Titulo",a);
    request.addProperty("Descripcion",b);
    request.addProperty("Compania",c);
    request.addProperty("Palabras",d);
    request.addProperty("Fecha",f);

    SoapSerializationEnvelope envelope =
            new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.dotNet=true;

    envelope.setOutputSoapObject(request);


    HttpTransportSE transporte = new HttpTransportSE(URL);

    try

    {
        transporte.call(SOAP_ACTION, envelope); 
        Log.v("prueba1", ">>>>>>>>>");

        SoapPrimitive resultado_xml = (SoapPrimitive) envelope.getResponse();
        String res = resultado_xml.toString(); 
        //My code in the .asmx return 1 if a row was added to the Table.

        if (!res.equals("1")) 
            resul = false;
    }

    catch(
    Exception e
    )

    {
        e.printStackTrace();
        resul = false;
    }

    return resul;
    }

    protected void onPostExecute(Boolean result) {


        if (result)
            txtResultado.setText("Insertado OK");
        else
            txtResultado.setText("Error!");
    }
}
}

EDIT1:WSDL(我只使用WebMethod“NuevoEvento”)

<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://proof.net/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://proof.net/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://proof.net/">
<s:element name="NuevoEvento">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Titulo" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Descripcion" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Compania" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Palabras" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Fecha" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="NuevoEventoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="NuevoEventoResult" type="s:int"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="ListadoEventos">
<s:complexType/>
</s:element>
<s:element name="ListadoEventosResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="ListadoEventosResult" type="tns:ArrayOfEventos"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfEventos">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Eventos" nillable="true" type="tns:Eventos"/>
</s:sequence>
</s:complexType>
<s:complexType name="Eventos">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="Id" type="s:int"/>
<s:element minOccurs="0" maxOccurs="1" name="Titulo" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Descripcion" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Compania" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Palabras" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Fecha" type="s:string"/>
</s:sequence>
</s:complexType>
<s:element name="NuevoEventoObjeto">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="evento" type="tns:Eventos"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="NuevoEventoObjetoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="NuevoEventoObjetoResult" type="s:int"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="NuevoEventoSoapIn">
<wsdl:part name="parameters" element="tns:NuevoEvento"/>
</wsdl:message>
<wsdl:message name="NuevoEventoSoapOut">
<wsdl:part name="parameters" element="tns:NuevoEventoResponse"/>
</wsdl:message>
<wsdl:message name="ListadoEventosSoapIn">
<wsdl:part name="parameters" element="tns:ListadoEventos"/>
</wsdl:message>
<wsdl:message name="ListadoEventosSoapOut">
<wsdl:part name="parameters" element="tns:ListadoEventosResponse"/>
</wsdl:message>
<wsdl:message name="NuevoEventoObjetoSoapIn">
<wsdl:part name="parameters" element="tns:NuevoEventoObjeto"/>
</wsdl:message>
<wsdl:message name="NuevoEventoObjetoSoapOut">
<wsdl:part name="parameters" element="tns:NuevoEventoObjetoResponse"/>
</wsdl:message>
<wsdl:portType name="ServicioClientesSoap">
<wsdl:operation name="NuevoEvento">
<wsdl:input message="tns:NuevoEventoSoapIn"/>
<wsdl:output message="tns:NuevoEventoSoapOut"/>
</wsdl:operation>
<wsdl:operation name="ListadoEventos">
<wsdl:input message="tns:ListadoEventosSoapIn"/>
<wsdl:output message="tns:ListadoEventosSoapOut"/>
</wsdl:operation>
<wsdl:operation name="NuevoEventoObjeto">
<wsdl:input message="tns:NuevoEventoObjetoSoapIn"/>
<wsdl:output message="tns:NuevoEventoObjetoSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ServicioClientesSoap" type="tns:ServicioClientesSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="NuevoEvento">
<soap:operation soapAction="http://proof.net/NuevoEvento" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="ListadoEventos">
<soap:operation soapAction="http://proof.net/ListadoEventos" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NuevoEventoObjeto">
<soap:operation soapAction="http://proof.net/NuevoEventoObjeto" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ServicioClientesSoap12" type="tns:ServicioClientesSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="NuevoEvento">
<soap12:operation soapAction="http://proof.net/NuevoEvento" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="ListadoEventos">
<soap12:operation soapAction="http://proof.net/ListadoEventos" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NuevoEventoObjeto">
<soap12:operation soapAction="http://proof.net/NuevoEventoObjeto" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ServicioClientes">
<wsdl:port name="ServicioClientesSoap" binding="tns:ServicioClientesSoap">
<soap:address location="http://localhost:50490/ServicioClientes.asmx"/>
</wsdl:port>
<wsdl:port name="ServicioClientesSoap12" binding="tns:ServicioClientesSoap12">
<soap12:address location="http://localhost:50490/ServicioClientes.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

EDIT1:我发布到服务的数据(仅使用第一个网络方法)

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Services;

namespace ServicioWebSoap
{
/// <summary>
/// Descripción breve de ServicioClientes
/// </summary>
[WebService(Namespace = "http://proof.net/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// Para permitir que se llame a este servicio web desde un script, usando      ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
// [System.Web.Script.Services.ScriptService]
public class ServicioClientes : System.Web.Services.WebService
{
    [WebMethod]
    public int NuevoEvento(string Titulo, string Descripcion, string   Compania, string Palabras, string Fecha)
    {
        SqlConnection con = new SqlConnection(@"Data  Source=SRCOMPUTADOR\SQLEXPRESS01;Initial Catalog=DBEventos;Integrated  Security=True");

        con.Open();

        string sql = "INSERT INTO Eventos (Titulo, Descripcion, Compania,  Palabras, Fecha) VALUES (@Titulo, @Descripcion, @Compania, @Palabras, @Fecha)";

        SqlCommand cmd = new SqlCommand(sql, con);

        cmd.Parameters.Add("@Titulo", System.Data.SqlDbType.NVarChar).Value =  Titulo;
        cmd.Parameters.Add("@Descripcion",  System.Data.SqlDbType.NVarChar).Value = Descripcion;
        cmd.Parameters.Add("@Compania", System.Data.SqlDbType.NVarChar).Value = Compania;
        cmd.Parameters.Add("@Palabras", System.Data.SqlDbType.NVarChar).Value = Palabras;
        cmd.Parameters.Add("@Fecha", System.Data.SqlDbType.NVarChar).Value = Fecha;

        int res = cmd.ExecuteNonQuery();

        con.Close();

        return res;
    }

    [WebMethod]
    public Eventos[] ListadoEventos()
    {
        SqlConnection con = new SqlConnection(@"Data  Source=SRCOMPUTADOR\SQLEXPRESS01;Initial Catalog=DBEventos;Integrated Security=True");

        con.Open();

        string sql = "SELECT id, Titulo, Descripcion, Compania, Palabras, Fecha FROM Eventos";

        SqlCommand cmd = new SqlCommand(sql, con);

        SqlDataReader reader = cmd.ExecuteReader();

        List<Eventos> lista = new List<Eventos>();

        while (reader.Read())
        {
            lista.Add(
                new Eventos(reader.GetInt32(0),
                            reader.GetString(1),
                            reader.GetString(2),
                            reader.GetString(3),
                            reader.GetString(4),
                            reader.GetString(5)));
        }

        con.Close();

        return lista.ToArray();
    }

    [WebMethod]
    public int NuevoEventoObjeto(Eventos evento)
    {
        SqlConnection con = new SqlConnection(@"Data Source=SRCOMPUTADOR\SQLEXPRESS01;Initial Catalog=DBEventos;Integrated Security=True");

        con.Open();

        string sql = "INSERT INTO Eventos (Titulo, Descripcion, Compania, Palabras, Fecha) VALUES (@Titulo, @Descripcion, @Compania, @Palabras, @Fecha)";

        SqlCommand cmd = new SqlCommand(sql, con);

        cmd.Parameters.Add("@Titulo", System.Data.SqlDbType.NVarChar).Value = evento.Titulo;
        cmd.Parameters.Add("@Descripcion", System.Data.SqlDbType.NVarChar).Value = evento.Descripcion;
        cmd.Parameters.Add("@Compania", System.Data.SqlDbType.NVarChar).Value = evento.Compania;
        cmd.Parameters.Add("@Palabras", System.Data.SqlDbType.NVarChar).Value = evento.Palabras;
        cmd.Parameters.Add("@Fecha", System.Data.SqlDbType.NVarChar).Value = evento.Fecha;

        int res = cmd.ExecuteNonQuery();

        con.Close();

        return res;
    }
}

}

1 个答案:

答案 0 :(得分:0)

答案是我的成功; WS只是一个仿真(在网络中不存在),解决方案在接下来的步骤中得到解决:

  1. 使用VS发布它并使用IIS管理器。
  2. 如果WS出现问题,请按照support.microsoft.com提供的说明真正有效(我搜索了几天,密钥就在那里)。
  3. 然后在HttpTransportSE呼叫的URL中使用您的本地IP替换&#34; localhost&#34;。