1

I am trying to connect Salesforce to Dotnet using the REST API. I am not sure whether Salesforce is connecting with Dotnet, I am getting errors. Here I have given the code for Apex and Dotnet. I have also set remote settings.

Error:Error message

Apex code:

public with sharing class NewsletterManager {
    public Contact currentContact;
    public Id id;

    public NewsletterManager(ApexPages.StandardController controller) {
        this.currentContact = (Contact)controller.getRecord();
        this.currentContact = [SELECT Name, Email, Id, Subscribe__c
                               FROM Contact
                               WHERE Id = :currentContact.Id];
        id = ApexPages.currentPage().getParameters().get('id');
    }

    public String getSubscriptionStatus() {
        HttpRequest request = new HttpRequest();

        //request.setMethod('GET');
        request.setEndpoint(
            'http:localhost' +
            currentContact.Email);
        request.setMethod('GET');
        Http http = new Http();
        HTTPResponse response = http.send(request);
        System.debug(response.getBody());
        return response.getBody();

        //return 'str';
    }

    public PageReference Subscribe() {
        SubscriptionRestCall('Subscribe');
        return null;
    }

    public PageReference Unsubscribe() {
        SubscriptionRestCall('Unsubscribe');
        return null;
    }

    private void SubscriptionRestCall(string action) {
        HttpRequest request = new HttpRequest();
        request.setMethod('GET');
        request.setEndpoint(    
            'http:localhost' +
            action + '/' +
            currentContact.Email);
            Http http = new Http();
            HTTPResponse response = http.send(request);
    }
}

DotNet:

using System.Collections.Generic;
using System.Web.Mvc;

namespace SalesforceToNet.Controllers
{
    public class NewsletterController : Controller
    {
        public static List<string> subscriptions =
        new List<string>();

        public ActionResult Status(string id)
        {
            string statusMessage =
            (subscriptions.Contains(id))
            ? "Subscription current."
            : "No subscription.";

            return View((object)statusMessage);    
        }

        public ActionResult Subscribe(string id)
        {
            //string str = "NEW Test";
           // return str;
            if (!subscriptions.Contains(id))
              subscriptions.Add(id);

            return View();    
        }

        public ActionResult Unsubscribe(string id)
        {
            subscriptions.Remove(id);

            return View();
        }
    }
}

1 Answer 1

3

You can't access 127.0.0.1 or "localhost" (your computer) from salesforce.com. 127.0.0.1 is always the "local machine", so trying to access 127.0.0.1 from Apex Code would try to access the salesforce.com server that the code is running on, not your computer. Instead, you need to access your computer through its network address (IP or domain name).

Try these steps:

  1. Login to your router (if possible), and configure your computer (e.g. 192.168.1.2) as the DMZ (demilitarized zone) computer. Alternatively, you can just enable port forwarding for ports 80 and 443 to your computer.

  2. Configure your computer's firewall to allow incoming connections from those ports.

  3. Determine your public IP address via some means (whatismyip.com is a popular choice), or your domain name, if you have one. DynDNS is one such option for dynamic domain names.

  4. Configure salesforce.com Remote Access settings to include the IP address or domain name from the prior step.

  5. Configure your code to use the same IP address or domain name.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .