已建立的连接是通过软件在主机中止(VS开发服务器) [英] An established connection was aborted by the software in your host machine (VS dev server)

查看:147
本文介绍了已建立的连接是通过软件在主机中止(VS开发服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题相近岗位,但没有涉及到使用Visual Studio开发服务器的ASP .NET。

我收到以下错误。

已建立的连接是通过软件在主机中止

和我执行下面的code:

 字符串employeesJson = client.downloadString(HTTP://本地主机:60000 /原料药/员工/ GetEmployees.aspx);

当我运行这个在常规网页浏览器(Chrome浏览器21或Internet Explorer 10),它运行得很好。我得到了我想要的JSON结果。

和正在使用我的 Web客户端类(变量客户端下)定义如下:

 公共类WebClient的{    私人的HttpClient HttpClient的;    公共Web客户端(){        HttpClient的=新DefaultHttpClient();    }    公共字符串downloadString(字符串URL)抛出IOException        HTTPGET GET =新HTTPGET(URL);        尝试{            HTT presponse响应= httpClient.execute(获取); //这是在错误发生。
            HttpEntity实体= response.getEntity();
            如果(实体!= NULL){                InputStream的流= entity.getContent();
                InputStreamReader中的StreamReader =新的InputStreamReader(流);                的BufferedReader的BufferedReader =新的BufferedReader(StreamReader的);
                StringBuilder的建设者=新的StringBuilder();                串线= NULL;
                尝试{
                    而((行= bufferedReader.readLine())!= NULL){
                        builder.append(行+\\ n);
                    }
                }赶上(IOException异常五){
                    e.printStackTrace();
                } {最后
                    尝试{
                        stream.close();
                    }赶上(IOException异常五){
                        e.printStackTrace();
                    }
                }                返回builder.toString();            }        //捕捉所有类型的异常此方法可以抛出的。捕捉异常被认为是坏的。
        }赶上(ClientProtocolException E){
            e.printStackTrace();
        }        返回null;    }}

我的Andr​​oidManifest.xml文件看起来是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=specialisering.android
    安卓版code =1
    机器人:=的versionName1.0>    <采用-SDK安卓的minSdkVersion =15/>
    <使用许可权的android:NAME =android.permission.INTERNET对>< /使用许可权>
    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>< /清单>


解决方案

我不能相信我没有寻求从文档的答案。它明确指出,127.0.0.1是模拟器本身的IP,而10.0.2.2是开发商的机器的IP。

从localhost 10.0.2.2更改解决了我的问题。

I've seen similar posts about this problem, but none of them relate to the use of the Visual Studio development server for ASP .NET.

I'm receiving the following error.

An established connection was aborted by the software in your host machine.

And I'm executing the following code:

String employeesJson = client.downloadString("http://localhost:60000/Api/Employee/GetEmployees.aspx");

When I run this in a regular webbrowser (Chrome 21 or Internet Explorer 10) it runs just fine. I get the JSON result that I want.

And my WebClient class being used (under the variable "client") is defined as follows.

public class WebClient {

    private HttpClient httpClient;

    public WebClient() {

        httpClient = new DefaultHttpClient();

    }

    public String downloadString(String url) throws IOException {

        HttpGet get = new HttpGet(url);

        try {

            HttpResponse response = httpClient.execute(get); //this is where the error occurs.
            HttpEntity entity = response.getEntity();
            if(entity != null) {

                InputStream stream = entity.getContent();
                InputStreamReader streamReader = new InputStreamReader(stream);

                BufferedReader bufferedReader = new BufferedReader(streamReader);
                StringBuilder builder = new StringBuilder();

                String line = null;
                try {
                    while ((line = bufferedReader.readLine()) != null) {
                        builder.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                return builder.toString();

            }

        //catch all the types of exceptions this method can throw. catching "Exception" is considered bad.
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }

        return null;

    }

}

My AndroidManifest.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="specialisering.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

解决方案

I can't believe I didn't seek an answer from the documentation. It clearly states that 127.0.0.1 is the IP of the emulator itself, while 10.0.2.2 is the IP of the developer machine.

Changing from localhost to 10.0.2.2 solved my issue.

这篇关于已建立的连接是通过软件在主机中止(VS开发服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆