在控制台应用程序中调用https Webservice [英] Call https Webservice in Console Application

查看:129
本文介绍了在控制台应用程序中调用https Webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在https:\\ test.com中托管了WCF网络服务:8080

现在我想在控制台应用中测试这个Web服务。

我收到以下错误



System.Web.Services.dll中出现未处理的System.Net.WebException类型异常



附加信息:请求失败,HTTP状态为401:未经授权。





我的代码是:





Hi,
I hosted the WCF webservice in https:\\test.com:8080
Now i Want to test this Webservice in Console Appliction.
I got the Following Error

An unhandled exception of type 'System.Net.WebException' occurred in System.Web.Services.dll

Additional information: The request failed with HTTP status 401: Unauthorized.


My code is:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using test1.ServiceReference1;
using test1.salesforce;
using System.Security.Cryptography.X509Certificates;
using System.ServiceProcess;
using System.Net;
namespace test1
{
    class Program
    {

        static void Main(string[] args)
        {
            System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();


        }
        public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
        {
            public TrustAllCertificatePolicy() {
                salesforce.Service1 sv = new salesforce.Service1();
                sv.GetAllData();
            }

            public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, int problem)
            {
                return true;
            }
        }

    }

}





这里Salesforce是我的Web参考。



Here Salesforce is my Web Reference.

推荐答案

为了能够测试和HTTPS WCF服务(无论客户端类型如何:控制台应用程序,Windows应用程序,Web应用程序等)您必须在两台计算机上安装有效的SSL证书(也可能由您生成测试证书):在客户端PC和服务器PC上(安装了WCF服务)。



首先你应该创建SSL证书然后你应该在两侧安装,最后你可以测试你的WCF服务。这是一个可以帮助您执行此操作的链接:具有自托管WCF的SSL服务 [ ^ ]
In order to can test and HTTPS WCF service (does't matter the client type: console application, windows application, web application, etc.) you must have installed valid SSL certificates (could be also test certificated generated by you) on both computers: on client PC and on server PC (where your WCF service is installed).

So first you should create the SSL certificates then you should installed in both side, and finally you could test your WCF service. Here is a link that can help you in doing this: SSL with Self-hosted WCF Service[^]


如果您使用代理,通常会发生授权失败。试试这个链接。祝你好运



http://forums.asp.net/t/1635458.aspx?Pass+credential+to+Web+service+Soap+ [ ^ ]

http://support.microsoft.com/kb/813834 [ ^ ]
Generally Authorization failure will come if you are using proxy. Try this links. Good luck

http://forums.asp.net/t/1635458.aspx?Pass+credential+to+Web+service+Soap+[^]
http://support.microsoft.com/kb/813834[^]


这篇关于在控制台应用程序中调用https Webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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